• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why does it take 2 requests to set session attribute?

 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a servlet filter and it checks the session to see if an object exists. If it doesn't, it creates it and saves it to the session and then works with the object. When I make the first request from a browser, it properly says it's null and then lets me set it (and a "getAttribute" check shows it's been set). Then in my browser, I fill out the form that's been returned, submit it and the filter says the session attribute is still null! It then returns to the browser for a redirect and THIS time says it's not null. What's going on?

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what your issue is, but the scoped variable comes into existence the moment you set it on the session. There is no "deferred set" going on. Look elsewhere for your problem.
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not sure either. I am calling request.getSession( true ) but that shouldn't be a problem, right? From what I understand it creates a new session only if one doesn't exist and then associates that new one with the request, right?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65129
92
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Personally, I never call anything but getSession() and let the container handle session management. I've never had a session problem.
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, I've gotten a little further but now I'm stumped and confused.

I changed the call to getSession() and the problem persists so i decided to print out the session itself. Upon the first request it prints:

org.mortbay.jetty.servlet.HttpSessionmanager$Session@1u4i9w

on the second request it prints:

org.mortbay.jetty.servlet.HttpSessionmanager$Session@3e5f2h

then guess what? On EVERY request after that it shows the same request as the second one:

org.mortbay.jetty.servlet.HttpSessionmanager$Session@3e5f2h

So, for some reason Jetty is not saving that first session to the request. Any idea why? Is it a bug or am I doing something? (My code only gets/sets that object and calls doFilter)
 
Majid Al-Fifi
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please show parts of the code.. what do you mean you set the object in session and then call doFilter? is the one in the chain?

please show some helpful peaces of your code?
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Majid Al-Fifi:
please show parts of the code.. what do you mean you set the object in session and then call doFilter? is the one in the chain?

please show some helpful peaces of your code?



I do show my code. It's in the first post. The only additional line of code in my filter's "doFilter" method (beyond what I posted above) is this line:

chain.doFilter( request, response );

That's it. There's no other code.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic