在javascript中读取会话变量? [英] Read session variable in javascript?
本文介绍了在javascript中读取会话变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在 servlet 中设置会话变量,并想在 javascript 中访问该变量.
I am setting session variable in servlet and want to access that variable in javascript.
ps = con.prepareStatement("select * from USERDETAILS where username=? and password=?");
ps.setString(1, username);
session.setAttribute("userName", username);
我在 javascript 函数中尝试过这些.但它不起作用...
I tried these in javascript function. but it wasn't working...
var name = ${userName};
var name = '<%= Session["userName"] %>';
推荐答案
看来你应该可以使用 getAttribute()
:
Seems you should be able to use getAttribute()
:
var name = '<%= session.getAttribute("userName") %>';
不过,这取决于运行文件的 Java 来替换嵌入的 <%= ... %>
,而在单独的 .js 中可能不会出现这种情况
文件.
Though, this depends on Java running through the file to replace the embedded <%= ... %>
, which probably won't be the case in separate .js
files.
这篇关于在javascript中读取会话变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文