Skip to content Skip to sidebar Skip to footer

Jsp - How To Pass A Javascript Var In Session.setattribute?

New to learning JSP, and trying out passing data between two pages. I'm wondering if it is possible to pass a javascript variable to session.setAttribute() At the moment, I can pa

Solution 1:

You cannot do that since javascript executes on client & JSP executes on server side.

If you want to set javascript variable to JSP session, then you pass this variable through the URL like this

varnumber = 7;
window.location="http://example.com/index.jsp?param="+number;

Now receive this var in your JSP page like this

Stringvar = request.getParameter("param");

Now set it in session

session.setAttribute("test", var);

EDIT :

varnumber = 7;
<%session.setAttribute("test", number);%>

In the above code, server will only execute the code inside <% %>. It does not know anything outside of the JSP tags. So, it will also dont know about your javascript variable number.

Server executes the code & the result will be sent to the browser, then your browser will execute that javascript code var number=7;.

Hope, now it is clear for you.

Post a Comment for "Jsp - How To Pass A Javascript Var In Session.setattribute?"