Win a copy of TDD for a Shopping Website LiveProject this week in the Testing forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

Why does it take 2 requests to set session attribute?

 
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Sheriff
Posts: 67634
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67634
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What's gotten into you? Could it be this tiny ad?
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic