tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Christopher Schultz <>
Subject Re: Null-pointer exception from response.encodeUrl under Windows Tomcat 7
Date Mon, 20 Jun 2011 16:02:13 GMT
Hash: SHA1


On 6/20/2011 6:12 AM, Calum wrote:
> On 15 June 2011 18:54, Konstantin Kolinko <> wrote:
>> The request and response objects must never be accessed outside the
>> request processing cycle.  The objects are recycled and cleared or
>> reused for subsequent request and responses, and are not guaranteed to
>> be thread-safe.  The consequences can be severe.
> I was interested by this.
> So:
>     private Service service;
>     @Override
>     public void init() throws ServletException {
>         super.init();
>         service = (Service)
> WebApplicationContextUtils.getWebApplicationContext(getServletContext()).getBean("service");
>     }
>     protected void processRequest(HttpServletRequest request,
> HttpServletResponse response)
>             throws ServletException, IOException {
>     service.logHit(request);
>     ....
>     }
> is a bad thing?

That depends on what Service.logHit does. If it only uses the
HttpServletRequest object during the method's lifetime, then everything
is fine. If it retains a reference to the request object, you will
probably end up with a problem.

> Could you explain why?

Tomcat re-uses the same request and response objects for a long time,
and so multiple request processor threads will see the same object over
the life of the server. If any of that code retains a reference to the
request, by the time the object is re-referenced by the same code for
whatever reason, the object will have "moved on" and will almost
certainly be invalid (from the perspective of the code that cached it)
and might /actually/ be invalid (because it is between requests and
Tomcat has actively invalidated it).

> I would assume that a new request object is created each time, and
> that while something has a reference to it, it will be fine, and when
> logHit has finished with it, it will be eligible for garbage
> collection.

Nope: see Konstantin's and me comments above.

- -chris
Version: GnuPG v1.4.10 (MingW32)
Comment: Using GnuPG with Mozilla -


To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message