Results 1 to 3 of 3

Thread: Beginners problem: Session variables

  1. #1
    Join Date
    Jul 2011
    Posts
    4

    Default Beginners problem: Session variables

    Hi there,

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

    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,
    Heiko

  2. #2
    Join Date
    Jul 2011
    Posts
    4

    Default

    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 at 09:15 AM.

  3. #3
    Join Date
    Jul 2011
    Posts
    4

    Default

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •