You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Bill Barker <wb...@wilshire.com> on 2003/10/01 05:11:33 UTC

Re: NullPointerException during HttpServletRequest.getSession

The short answer is that you can't do this.  It's true that Tomcat 3.2.x
allowed you to get away with nasty hacks like this, but in subsequent Tomcat
versions the Request object is only valid for the lifetime of the request.

"Dave Barkan" <db...@pcbi.upenn.edu> wrote in message
news:Pine.LNX.4.44.0309301556110.23437-100000@snowball.pcbi.upenn.edu...
> OK thanks for responding to my first question; I looked around, some
> interesting discoveries within:
>
> On Tue, 30 Sep 2003, Shapira, Yoav wrote:
> >
> > Howdy,
> >
> > >The symptom is a NullPointerException when I call getSession() on an
> > >HttpServletRequest object.  The HttpServletRequest object had
> > previously
> > >been saved as an attribute of the Session that it contained.
> >
> > This seems prone to problems.  How can storing the request itself as a
> > session attribute be valid?  A session by definition may have many
> > requests in it -- so you override the value of the attribute when a new
> > request comes in?
>
> This is true; although everything had worked previously until we upgraded
> to the new tomcat specifications.  However the same behavior occurs when I
> hacked by saving the HttpServletRequest in a global variable for later
> use.  Also, see below:
>
> >
> > >I tried looking at the CoyoteRequestFacade code but the directory
> > >containing it in the public download site was empty!  That was the code
> > >that threw the actual exception so I'm sure it could give some insight.
> >
> > It's here:
> > http://cvs.apache.org/viewcvs/jakarta-tomcat-connectors/coyote/src/java/
> > org/apache/coyote/tomcat4/CoyoteRequestFacade.java?rev=1.3&content-type=
> > text/vnd.viewcvs-markup
> >
> > However, it's been modified since the last release so the line number
> > may not be accurate.  Anyways, it's likely the request object itself had
> > been nulled out, giving NPE in request.getSession(....).
> >
> Yes I think so as well, that looks like the only thing that can throw the
> NPE.  I'm wondering where the nullification could have taken place, since
> the CoyoteRequestFacade is still intact, I will look around.  However, I
> noticed this wile looking at the CoyoteRequestFacade code:
>
> CoyoteRequestFacade.java:
>
> public HttpSession getSession(boolean create) {
>         return request.getSession(create);
> }
>
> CoyoteRequest.java:
> /**
>  * Return the session associated with this Request, creating one
>  * if necessary and requested.
>  *
>  * @param create Create a new session if one does not exist
>  */
>  public HttpSession getSession(boolean create) {
>       return null;
>  }
>
> This was in the cvs repository; if this is an accurate reflection of the
> library I'm using then it seems I am always getting a null Session.
> However I think the CoyoteRequest being null is actually throwing the
> error, rather than the session, so either something is nullifying the
> CoyoteRequest, or that cvs code is not the code I'm using, or both.
>
> Thanks for your help!
> Dave Barkan




---------------------------------------------------------------------
To unsubscribe, e-mail: tomcat-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tomcat-user-help@jakarta.apache.org