Hi,

I'll wager that you've cached your session object between requests and the HttpSessionFacade object
has been 'recycled'.

I got horribly burned by this in some code I wrote - I was caching the session object in the ServletContext (naughty)
and, if the session object was non-null, I tried to fetch a value from the session.

The problem was that the vital link between the HttpSessionFacade object and the 'real' session object had been broken.
The facade object couldn't 'delegate' the getValue() call to the real session object.

Costin has developed some fixes for this in the 3.3 code line and the same behavior isn't present in tomcat 4.x.
However, tomcat 3.2 does suffer from this problem.

I put together a hack for 3.2.x to get around this problem - in the org.apache.tomcat.facade.HttpSessionFacade.java
I replaced the original getSession() method with the following method. As you can see, all I did was to return the 'real' HttpSession
instead of the session facade. Costin's assistance in achieving this solution was invaluable.

/** begin code fragment ***/

       public HttpSession getSession(boolean create) {
             HttpSession realSession = request.getSession( create );

            // test to see if we're using this 'hack' or original behaviour...
            String useSessionFacade = System.getProperty("tomcat.useSessionFacade");

            if ( (useSessionFacade == null) ||
                 (useSessionFacade.compareToIgnoreCase("false")==0) ) {
            //
            // use default behavior (return 'real' session object)
            //
              return realSession;
            }
            else {
            /*
             * return to original tomcat 3.2.1 behaviour - use a sessionFacade
             */
       if( realSession == null ) {
         if( sessionFacade!= null) sessionFacade.recycle();
             return null;
       }
       if(sessionFacade==null)
         sessionFacade=new HttpSessionFacade();
         sessionFacade.setRealSession( realSession );
         return sessionFacade;
      }
    }

/*** end code fragment ***/

Alternative:
For those loathe to build a different version of tomcat 3.2.x,  that the following coding approach helped avoid this problem
at least for my situation, (essentially I got rid of the caching).
Sorry. but it's yet another awful counter example [must rank up there with those "Shape" examples from the GoF pattern book ;-) ]
This one sticks a CountMe object into the session object.
The call to session.isNew() determines whether the session object is a brand new session object, or whether I've just picked
up the already existing one.

Note, programmatically this does the same as caching the session object, it's just a little bit different. It won't do for all cases
but, in order to workaround this problem it worked fine for me ;-)
 

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    response.setContentType(CONTENT_TYPE);
    PrintWriter out = response.getWriter();
    out.println("<html>");
    out.println("<head><title>SessionTest</title></head>");
    out.println("<body>");
    HttpSession session = request.getSession(true);
    if ( session.isNew() ) {
      out.println("Created brand new session for this request");
    }
    CountMe ctr = (CountMe)session.getAttribute("Counter");
    if ( ctr == null ) {
      out.println("no counter associated with session - creating new one");
      ctr = new CountMe();
      ctr.increment();
      session.setAttribute("Counter",ctr);
    }
    else {
      ctr.increment();
      session.setAttribute("Counter",ctr);
    }
    out.println("Current value of (Session)counter = " + ctr.getCounter());
    out.println("</body></html>");
  }
 
 

Noone Anil Kumar wrote:

Here is the trace:

java.lang.NullPointerException
        at org.apache.tomcat.facade.HttpSessionFacade.getValue(Compiled Code)
        at org.apache.tomcat.facade.HttpSessionFacade.getValue(Compiled Code)
        at SessionTrack.run(Compiled Code)
        at java.lang.Thread.run(Compiled Code)

In SessionTrack class i am trying to retrive the values(mode,lastAcessTime ,IdleTime etc)  which are stored in the HttpSession Object.
 

Noone Anil Kumar wrote:

Yes, i was using HttpServletRequest.getSession() function call inside getSession( req )
function, It's working fine with Servlet runner but not with tomcat as i said ...

William Kaufman wrote:

 
Is there any difference in :  Jsdk2.0 HttpSession Object & the above one ???? 
org.apache.tomcat.facade.HttpSessionFacade IS an HttpSession.
So whenever we try to access the current session object,   it seems that we get NULL object in Tomcat.
I don't know what getSession(HttpServletRequest) is--it doesn't seem to be in the JSDK 2.0.
You should call HttpServletRequest.getSession()  -- if you still have trouble, the problem is somewhere else.  You should check where that stack trace is coming from.
                                                            -- Bill K.

--
http://www.borland.com/newsgroups
http://www.borland.com/devsupport/disclaim.html
 

Reply via email to