var 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
.
Question
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
Solution
var 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
.