Servlet Event Listeners: Did you know?

11 Mar

… you could automatically register any number of servlet event listeners (ServletContextListener, HttpSessionListener, ServletRequestListener, and more …) without any additional web.xml line?

The Java Server Pages 2.0 specification dictates that all .tld files inside META-INF directories, from jars in the web application classpath (WEB-INF/lib), must be read and parsed by the Servlet Container (JSP.7.1.9 – Event Listeners).

Fortunately, .tld files accept the <listener> element. What about creating reusable jars (web frameworks?) and do their setup inside such listeners, as Sun JSF RI (Mojarra) is doing:

<!-- jsf-impl.jar!/META-INF/jsf_core.tld -->
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
  version="2.1">

  <description>
    The core JavaServer Faces custom actions that are independent of
    any particular RenderKit.
  </description>
  <tlib-version>1.2</tlib-version>
  <short-name>f</short-name>
  <uri>http://java.sun.com/jsf/core</uri>

  ...

  <!--
    This ServletContextListener initializes the runtime environment
    of the JavaServer Faces Reference Implementation when a web
    application including it is initialized by the container.
  -->
  <listener>
    <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
  </listener>

  ...
</taglib>

That said, why am I still having to add <listener> from web frameworks in my web.xml?

6 Responses to “Servlet Event Listeners: Did you know?”

  1. Marcelo Ricardo Bueno de Campos March 13, 2008 at 8:07 pm #

    It’s so interesting now, where I work we have a private “web framework”, I’ll try this one.

    Thanks a lot.

  2. Fernando Boaglio March 15, 2008 at 10:31 am #

    I wish I could do the same for servlets and filters!

  3. Fabio Kung March 17, 2008 at 1:13 pm #

    This buggy wordpress installation lost my post last paragraph!

    It should be the question:
    “Why isn’t there ServletContext.addServlet(Class< ? extends Servlet>) ?”

  4. Rubem Azenha March 24, 2008 at 1:37 pm #

    “That said, why am I still having to add from web frameworks in my web.xml?”

    Because if you do like you proposed, it will be dificult to change the url “extensions” :)

    Ok, thats probably a very bad excuse!

  5. Fabio Kung March 24, 2008 at 1:52 pm #

    - ServletContext.addServlet(Class< ? extends Servlet>, String urlPattern)
    - @Servlet(“*.do”)
    :P

  6. Tiago Albineli Motta April 23, 2008 at 4:12 pm #

    It’s a good tip

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.