Announcement Announcement Module
No announcement yet.
Beginners problem: Session variables Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Beginners problem: Session variables

    Hi there,

    i want to save some variables in a session inside a spring controller, so here's the code:

    HttpSession sess = request.getSession( true );
    if ( sess == null ) {
    	logger.debug( "session is null" );
    String au = (String) sess.getAttribute("myVar");
    if ( au != null ) {
    	logger.debug( au );
    sess.setAttribute( "myVar", "foo session message " + System.currentTimeMillis() );
    running on tomcat this gives me on the first access nothing, on the second it gives me 'foo session message 123...', so far so good, but on the third access, this throws a null pointer exception on the getAttribute(), because sess is null

    What am i doing wrong, or is another way to achieve this?

    Thank you,

  • #2
    update 1 (obsolete): the described behaviour is actually only happening, if i use the internal browser from eclipse. if i access the page with firefox, the null pointer exception comes at the first access

    update 2: it is not, as assumed with update 1 - browser dependent, i just have to restart tomcat from then its always the first 3 times i access the controller, no matter what browser, after that i can use another browser at it fails from first access until i restart tomcat again
    Last edited by hisn; Aug 8th, 2011, 09:15 AM.


    • #3
      i found it.

      i did this in a helper class which holds a local copy of HttpServletRequest and was used in a singleton. therefor some kind of session sharing happened. i still do not understand, why i had 3 attempts until i have to restart tomcat, shouldn't i end up with several users sharing the same session in this case?

      anyway, if i do not use this helper class as singleton, but instantiate it, everyone gets its own session and it works.