最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jsp - Accessing values stored in HttpSession using JavaScript - Stack Overflow

programmeradmin4浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 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.

发布评论

评论列表(0)

  1. 暂无评论