在javascript中读取会话变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18009370/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 21:45:28  来源:igfitidea点击:

Read session variable in javascript?

javajavascriptsessionvariables

提问by Bobby Rachel

I am setting session variable in servlet and want to access that variable in javascript.

我正在 servlet 中设置会话变量,并希望在 javascript 中访问该变量。

 ps = con.prepareStatement("select * from USERDETAILS where username=? and password=?");
 ps.setString(1, username);
 session.setAttribute("userName", username);

I tried these in javascript function. but it wasn't working...

我在 javascript 函数中尝试了这些。但它不起作用......

var name = ${userName};
var name = '<%= Session["userName"] %>';

采纳答案by Jonathan Lonowski

Seems you should be able to use getAttribute():

似乎你应该能够使用getAttribute()

var name = '<%= session.getAttribute("userName") %>';

Though, this depends on Java running through the file to replace the embedded <%= ... %>, which probably won't be the case in separate .jsfiles.

不过,这取决于运行文件的 Java 来替换嵌入的<%= ... %>,而在单独的.js文件中可能不会出现这种情况。

回答by Amadan

Unless your session is completely stored in a cookie, you cannot read a session variable in JavaScript. You should store the variable contents in a JavaScript variable during page generation, or use AJAX to fetch it later.

除非您的会话完全存储在 cookie 中,否则您无法在 JavaScript 中读取会话变量。您应该在页面生成期间将变量内容存储在 JavaScript 变量中,或者稍后使用 AJAX 获取它。

回答by Sunny Sharma

Try using this codeto access session:

尝试使用此代码访问会话:

var myName= '<%= Session["myName"]%>';

回答by Yaroslav Yakovlev

Did you check in the debugger(javascript debugger) what name contained?
Did you try to assign name to some html, to make sure data got assigned correctly?
Did you try to remove quotes?
also your code should be on the main page, so that server engine go through it and perform substitution of Session("userName") to the actual value.

您是否检查了调试器(javascript debugger)包含的名称?
您是否尝试为某些 html 分配名称,以确保正确分配数据?
您是否尝试删除引号?
您的代码也应该在主页面上,以便服务器引擎通过它并执行 Session("userName") 到实际值的替换。

回答by MGoksu

for those who get an error with the code below about characters like <% vs.

对于那些在下面的代码中遇到关于 <% vs. 等字符的错误的人

var name = '<%= session.getAttribute("username") %>';

I had the same issue but it turned out that I placed the script in a wrong place in the code.

我遇到了同样的问题,但结果是我将脚本放在了代码中的错误位置。

So you better check where you put the code.

所以你最好检查一下你把代码放在哪里。

Hope this helps

希望这可以帮助

回答by Yash P Shah

try this -> {sessionScope.username}

试试这个 -> {sessionScope.username}

By default page, request, session and application objects are available to JSP pages. So you can access then using EL syntax.

默认情况下,JSP 页面可以使用页面、请求、会话和应用程序对象。因此,您可以使用 EL 语法进行访问。

And following table shows IMPLICIT objects available to EL.

下表显示了 EL 可用的 IMPLICIT 对象。

       Implicit object            Description
1.     pageScope        Scoped variables from page scope
2.     requestScope     Scoped variables from request scope
3.     sessionScope     Scoped variables from session scope
4.     applicationScope Scoped variables from application scope
5.     param            Request parameters as strings
6.     paramValues      Request parameters as collections of strings
7.     header           HTTP request headers as strings
8.     headerValues     HTTP request headers as collections of strings
9.     initParam        Context-initialization parameters
10.    cookie           Cookie values
11.    pageContext      The JSP PageContext object for the current page

Reference: Are session and sessionScope the same in JSP EL?

参考:JSP EL 中的 session 和 sessionScope 是否相同?