users@servlet-spec.java.net

[servlet-spec users] Re: Clarification of ServletRequestListener

From: Mark Thomas <markt_at_apache.org>
Date: Wed, 17 Dec 2014 11:46:39 +0000

On 16/12/2014 14:56, Greg Wilkins wrote:
>
> On 16 December 2014 at 10:19, Jozef Hartinger <jozefhartinger_at_gmail.com
> <mailto: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.

The point that needs to be clarified in that case then is whether or not
the request and response exist the scope of the web application when
they enter async mode. Given that you can call
request.getServletContext() and get the ServletContext it seems wrong to
say that the request has left the scope of the web application.

This comes back to your earlier point about clarifying the behaviour of
the Request and Response methods when in async mode. Clearly there is a
lot of work to be done here.

Mark