tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "T. Park" <>
Subject Re: Tomcat Sessions
Date Fri, 06 Apr 2001 23:09:33 GMT

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
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
            String 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
       if( realSession == null ) {
         if( sessionFacade!= null) sessionFacade.recycle();
             return null;
         sessionFacade=new HttpSessionFacade();
         sessionFacade.setRealSession( realSession );
         return sessionFacade;

/*** end code fragment ***/

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 {
    PrintWriter out = response.getWriter();
    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
      ctr = new CountMe();
    else {
    out.println("Current value of (Session)counter = " +

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 Code)
>         at 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.
>> >

View raw message