• Post Reply Bookmark Topic Watch Topic
  • New Topic

I need to store a value for just the life of a http session

 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I need to store a value for just the life of a http session. Does Servlets, JSP, struts have an easy way to do this? Or do I have to create my own session variable persistence functionality? Or can some one point me to somewhere that might have already done this type of functionality?
Thanks in advance
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's what session scope is all about. Create a session-scoped variable using the setAttribute() method of HttpSession.
 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I did that but it did not seem to work in my environment. What I am trying to do is have an acknowlegdement popup when you first hit the site for a session. Once they click on the acknowledge button I send an ajax call to a servlet that then calls request.getSession().setAttribute("acknowledged", true). The hope was that from then on the acknowledge popup would see the session variable on every page and not popup. But as soon as I go to another jsp page it pops back up. What am I doing wrong? When I step through the debug it calls the setAttributte, but when it comes to the next page the getAttribute returns null;

I have a call on the struts tiles template to check request.getSession().getAttribute("acknowledged") to tell the popup whether to activate or not.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, that's how it should work. What you are doing wrong cannot be ascertained until we see code.

If the code is full of Struts markup, I'll be happy to move this along to the Struts forum. If it's pure JSP code, I'll move it to the JSP forum. If it's in a servlet controller, it'll be fine here. (And I fervently hope you are not putting Java code in your JSPs.)
 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ahhh! I forgot to put the ${} around my variable in this



It works now. Sorry for my crazy brain.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sometimes all it takes is trying to explain it to someone to see the issue.

But... is that Java code inside the JSTL tags? Or JavaScript?

If the latter, this could be simplified to:
 
Ryan Tracy
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The code above is javascript. If I do what you have there, what would the result be if ${acknowledged} is null because it has not been set yet?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ryan Tracy wrote:The code above is javascript. If I do what you have there, what would the result be if ${acknowledged} is null because it has not been set yet?


I would first argue that it's the job of the controller to make sure that data is good before it gets to the JSP.

But if null is significant, meaning to skip the statement, then:

If it's to mean the same as false then:
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!