Here is my Spring MVC Controller code:
session.setAttribute("YourProperty", "arg1");
How can I access an attribute stored in my HttpSession using JavaScript?
I have tried using this code:
var property = <%=session.getAttribute("YourProperty")%>;
alert(property);
But it returns null.
Thanks
Here is my Spring MVC Controller code:
session.setAttribute("YourProperty", "arg1");
How can I access an attribute stored in my HttpSession using JavaScript?
I have tried using this code:
var property = <%=session.getAttribute("YourProperty")%>;
alert(property);
But it returns null.
Thanks
Share Improve this question edited Jan 2, 2014 at 16:33 informatik01 16.5k11 gold badges79 silver badges108 bronze badges asked Nov 22, 2013 at 7:44 edaklijedaklij 4,25111 gold badges34 silver badges43 bronze badges 2-
First of all you're being unsafe with the assignment to
property
. If it were a string the code would fail. The other thing is your set attribute myProperty on the session but you're getting YourProperty which doesn't match. – Bart Commented Nov 22, 2013 at 7:47 - sorry it was my mistake..now i have edited my question.. – edaklij Commented Nov 22, 2013 at 7:56
1 Answer
Reset to default 4var property="<%=session.getAttribute("MyProperty")%>";
alert(property);
Attribute names should match and since you are adding a string, you should add "
around <%=session.getAttribute("MyProperty")%>
, and the code will alert arg1
.