7

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);

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

var name = ${userName};
var name = '<%= Session["userName"] %>';
Bobby Rachel
  • 453
  • 6
  • 10
  • 20
  • possible duplicate of [accessing session variables in javascript inside jsp](http://stackoverflow.com/questions/7241442/accessing-session-variables-in-javascript-inside-jsp) – Rohit Jain Aug 02 '13 at 05:34
  • btw what is the server side language you are using? – Gnanz Aug 02 '13 at 05:34

6 Answers6

9

Seems you should be able to use 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 .js files.

Jonathan Lonowski
  • 121,453
  • 34
  • 200
  • 199
  • i'm using a seprate .js file and whe i use `var name = '<%= session.getAttribute("userName") %>';` i checked the value of var name and it was `<%= session.getAttribute("userName") %>` instead of the value of it ! What is wrong ? – Poorya Nov 10 '13 at 08:14
  • @pouria Questions really shouldn't be posted in comments. http://stackoverflow.com/questions/ask and link to this Q&A if you would like to reference it. But, in short, JSP isn't parsing the file to perform any substitutions. It's being treated as static, just like any images, and sent as-is to the browser. – Jonathan Lonowski Nov 10 '13 at 08:21
  • 1
    thanks for heads up ! would you mind checking [this](http://stackoverflow.com/questions/19887753/how-to-add-hidden-attribute-to-dom-using-servlet) – Poorya Nov 10 '13 at 08:38
2

Try using this code to access session:

var myName= '<%= Session["myName"]%>';
Sunny Sharma
  • 4,688
  • 5
  • 35
  • 73
  • very old though, this is what i was looking for, i wasted 2 hours before coming here . Thank you very much. – N Khan Feb 01 '17 at 06:28
1

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.

Amadan
  • 191,408
  • 23
  • 240
  • 301
0

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.

Yaroslav Yakovlev
  • 6,303
  • 6
  • 39
  • 59
0

for those who get an error with the code below about characters like <% 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

MGoksu
  • 510
  • 6
  • 13
0

try this -> {sessionScope.username}

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

And following table shows IMPLICIT objects available to EL.

       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?

Yash P Shah
  • 779
  • 11
  • 15