5

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

informatik01
  • 16,038
  • 10
  • 74
  • 104
edaklij
  • 4,121
  • 11
  • 31
  • 43
  • 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 Nov 22 '13 at 07:47
  • sorry it was my mistake..now i have edited my question.. – edaklij Nov 22 '13 at 07:56

1 Answers1

4
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.

Subin Sebastian
  • 10,870
  • 3
  • 37
  • 42