Jump to: navigation, search


< Jetty‎ | Feature
Revision as of 00:38, 9 June 2010 by Janb.webtide.com (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)


The 2.5 Servlet Specification adds the ability to inject JNDI resources into fields and methods of servlets, filters and listeners, and also to perform certain callbacks at various points in the lifecycle of a web application.

JNDI resource injection and the lifecycle callbacks can be specified entirely within the web.xml file, or alternatively marked up as annotations in your source code. You can even use a combination of annotations and web.xml declarations.

One important thing to be aware of is that the 2.5 Servlet Specification introduced a new attribute into the <web-app> element, the metadata-complete attribute. If true, then the web container will NOT search the webapp for source code annotations, and your web.xml file must contain all resources required. If false or not specified, then jetty is required to examine all servlets, filters and listeners in the webapp for annotations. Therefore, you can save startup time by using this attribute correctly - if you don't want to use annotations then ensure you mark metadata-complete="true", otherwise you will pay the penalty of the code examination.

Here's an example of setting this attribute:

   xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd" 


Servlet 2.5 Annotations

  • @Resource equivalent to resource-ref, resource-env-ref, env-entry and message-destination-ref in web.xml
  • @Resources declares java:comp/env name linkages for reference by JNDI lookups
  • @PostConstruct equivalent to post-construct in web.xml
  • @PreDestroy equivalent to pre-destroy in web.xml
  • @RunAs equivalent to run-as in web.xml

Resource Injections

Resource injections make available an object from JNDI as the value of a field or method parameter of a servlet, filter or listener class.

We'll look at a couple of small examples of how to declare them in web.xml and how to declare them in code, but for more in-depth information, you should consult the Common Annotations for the Java Platform Specification (JSR250), the Servlet 2.5 Specification (JSR154) and the JavaEE Specification v5 (JSR244).

Here is a small example of the injection of a DataSource resource via a web.xml declaration:


This example shows that the resource named java:comp/env/jdbc/mydatasource will be injected by Jetty into the the field named myDatasource or the method named setMyDatasource(javax.sql.DataSource) in the instance of the class com.acme.MyServlet before it goes into service.

The equivalent injection using an annotation on the field instead would be:

import javax.sql.DataSource;
import javax.annotation.Resource;
public class MyServlet extends HttpServlet
    @Resource (name="jdbc/mydatasource", type=DataSource.class)
    private DataSource foo;

The equivalent as an annotation on the method would be:

import javax.sql.DataSource;
import javax.annotation.Resource;
public class MyServlet extends HttpServlet
    private DataSource foo;
    @Resource (name="jdbc/mydatasource", type=DataSource.class)
    private void setMyDatasource (DataSource ds)
        foo = ds;