jsr340-experts@servlet-spec.java.net

[jsr340-experts] About SERVLET_SPEC 38

From: Shing Wai Chan <shing.wai.chan_at_oracle.com>
Date: Mon, 23 Jul 2012 15:16:10 -0700

I would like to discuss
http://java.net/jira/browse/SERVLET_SPEC-38, " Listeners were not
invoked in a random order prior to Servlet 3.0 spec".

The following are some info:
=====
Servlet 2.5 mr2
---------------
In 10.3.3 (p78),
The Web container registers the listener instances according to the
interfaces they implement and the order in which they appear in the
deployment descriptor. During Web application execution, listeners are
invoked in the order of their registration.

In 10.3.4 (p78),
On application shutdown, listeners are notified in reverse order to
their declarations with notifications to session listeners preceeding
notifications to context listeners.


Servlet 3.0
---------
In 1.6.1 (p.4),
Prior to this release of the specification, listeners were invoked in a
random order. As of servlet 3.0, the order in which listeners are
invoked is defined in "Assembling the descriptor from web.xml,
web-fragment.xml and annotations" on page 8 76

In 11.3.3 (p.112), we have the same paragraph as in 10.3.3 of Servlet
2.5 mr2 (as above).
In 11.3.4 (p.112), we have the same paragraph as in 10.3.4 of Servlet
2.5 mr2 (as above).

In p.72 c,
c. Prior to this release of the specification, context listeners were
invoked in random order. As of Servlet 3.0, the listeners are invoked in
the order in which they are declared in the web.xml as specified below:
     i. Implementations of javax.servlet.ServletContextListener are
invoked at their contextInitialized method in the order in which they
have been declared, and at their contextDestroyed method in reverse order.
     ii. Implementations of javax.servlet.ServletRequestListener are
invoked at their requestInitialized method in the order in which they
have been declared, and at their requestDestroyed method in reverse order.
     iii. Implementations of javax.servlet.http.HttpSessionListener are
invoked at their sessionCreated method in the order in which they have
been declared, and at their sessionDestroyed method in reverse order.
     iv. The invocation order for any of the other listener interfaces
is unspecified.

Comments:
1. 10.3.3 of Servlet 2.5 mr2 = 11.3.3 of Servlet 3.0
2. 10.3.4 of Servlet 2.5 mr2 = 11.3.4 of Servlet 3.0
3. 11.3.3 and 10.3.4 seems to imply p.77c i, ii, iii.
    But p.77c i, ii, iii are more precise.
    Suppose contextListener1, sessionListener1, contextListener2 are
defined in this order in web.xml.
    Since context is always initialized before sessions are created, the
listeners are invoked as follows:
     contextListener1, contextListener2, [sessionListener1]*
    In other words, the Servlet 3.0 is a clarification of implicit
assumption in the previous spec.
    (But 1.6.1 is not correct in this case.)
4. p.72c iv seems to contradict to 11.3.3 for
ServletContextAttributeListener, ServletRequestAttributeListener and
HttpSessionAttributeListener.

Proposed Changes:
1. *remove 1.6.1* (p.4) as 1.6 is about backward compatibility
2. In 11.3.3,
The Web container registers the listener instances according to the
interfaces they implement and the order in which they appear in the
deployment descriptor. During Web application execution, listeners
*corresponding to a given event* are invoked in the order of their
registration.
3. In p.77 c,
c. *The listeners are invoked in the order in which they are declared in
the web.xml as specified below: *
    i. ... (same)
    ii. ... (same)
    iii. ... (same)
    iv. *The methods of implementation of
javax.servlet.ServletContextAttributeListener,
**ServletRequestAttributeListener and HttpSessionAttributeListener**are
invoked in the order in which they have declared when the corresponding
events are fired.
*
=====
Please let me know if you have any comments.
Thanks.
      Shing Wai Chan