users@servlet-spec.java.net

[servlet-spec users] Re: Clarification of ServletRequestListener

From: Greg Wilkins <gregw_at_intalio.com>
Date: Tue, 16 Dec 2014 15:56:29 +0100

On 16 December 2014 at 10:19, Jozef Hartinger <jozefhartinger_at_gmail.com>
wrote:
>
> the behavior of *javax.servlet.ServletRequestListener* seems to be
> underspecified in Servlet 3.1 especially in case of asynchronous requests.
> I am seeking clarification for the case that follows.
>

Jozef,

A good summary of the issue. I'll have to search the archives but I do
recall this being discussed before and it was decided that despite the
names of the listener methods, they should be invoked as per your first
example - specifically each time the servlet dispatches a thread to the
context these listeners should be called.

The reasoning for this comes from the ability to do cross context
dispatches, both with synchronous and asynchronous APIs. Remember that
the RequestListener is a artefact of each context. Consider a setup with
3 contexts, A, B & C and the following events take place:

   1. request dispatched to context A
   2. A RequestDispatcher.forward is used to invoke context B
   3. startAsync is called on the request
   4. the thread returns to the container.
   5. an asynchronous thread calls AsyncContext.dispatch(ContextC,path)
   6. the thread is dispatched to context C
   7. the response is generated
   8. the thread returns to the container.

In this example, if each context has a request listener installed then
calling order should be:

   - A.requestInitialized
   - B.requestInitialized
   - B.requestDestroyed
   - A.requestDestroyed
   - C.requestInitialized
   - C.requestDestroyed

Again, we'll have to lookup the archives to see when/where this was
decided, but I'm 99% sure that was the decision made. Obviously we have
not documented that well enough, however I do note the javadoc on the
listener:

    /**
     * Receives notification that a ServletRequest is
     * about to go out
     * of scope of the web application.
     *
     * @param sre the ServletRequestEvent containing
     * the ServletRequest
     * and the ServletContext representing the
     * web application
     */
    public void requestDestroyed(ServletRequestEvent sre);

    /**
     * Receives notification that a ServletRequest is
     * about to come
     * into scope of the web application.
     *
     * @param sre the ServletRequestEvent containing
     * the ServletRequest
     * and the ServletContext representing the web
     * application
     */
    public void requestInitialized(ServletRequestEvent sre);

So note that it talks about entering and exiting the scope of the
webapplication and not the initialization/destruction of the request
objects.

cheers




-- 
Greg Wilkins <gregw_at_intalio.com>  @  Webtide - *an Intalio subsidiary*
http://eclipse.org/jetty HTTP, SPDY, Websocket server and client that scales
http://www.webtide.com  advice and support for jetty and cometd.