This week's book giveaway is in the Design and Architecture forum.
We're giving away four copies of Communication Patterns: A Guide for Developers and Architects and have Jacqui Read on-line!
See this thread for details.
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

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: 67750
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: 67750
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.
 
Yeah. What he said. Totally. Wait. What? Sorry, I was looking at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic