wood burning stoves 2.0*
The moose likes Servlets and the fly likes Why does it take 2 requests to set session attribute? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Why does it take 2 requests to set session attribute?" Watch "Why does it take 2 requests to set session attribute?" New topic
Author

Why does it take 2 requests to set session attribute?

Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
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

Joined: Jan 10, 2002
Posts: 60804
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
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

Joined: Jan 10, 2002
Posts: 60804
    
  65

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

Joined: Feb 25, 2003
Posts: 387
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

Joined: Aug 22, 2006
Posts: 45
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?


SCJP1.4, SCWCD1.4
Dan Bizman
Ranch Hand

Joined: Feb 25, 2003
Posts: 387
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.
 
 
subject: Why does it take 2 requests to set session attribute?
 
Similar Threads
How to make Filter getting called only once for every User
finalize() problem
Test question
IllegalStateExceptions, null ponter exceptions, ServletExceptions and other odd servlet behavior
How could a session attribute go missing without using removeAttribute()