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

Was it helpful?

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top