Jetty has moved!
Jetty is a project at the Eclipse Foundation.
Homepage:http://www.eclipse.org/jetty
Downloads: http://download.eclipse.org/jetty/
Documentation:http://www.eclipse.org/jetty/documentation/current/
About:http://www.eclipse.org/jetty/about.php
Jetty Powered:http://www.eclipse.org/jetty/powered/
Contact the core Jetty developers at www.webtide.com
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery
Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 8 Next »

Servlet 2.5 Annotations, Resource Injections and Callbacks

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.

First, lets look at how to use web.xml for injections and callbacks.

Resource Injections and Callbacks specified in web.xml

Introduction

The Servlet 2.5 web.xml schema has been augmented to support the declaration of resource injections and lifecycle callbacks. We'll look at a couple of small examples of how to declare them in web.xml, 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:

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.

Similarly, values for <env-entry>s can be injected:

This example shows the value 99.99 being injected into the field named maxAmount (or the method named setMaxAmount(Double)) in the instance of the class com.acme.MyServlet before it goes into service.

The Servlet 2.5 Specification (JSR154) also introduces the concept of lifecycle callbacks. These are of two types: a post-construction callback, and a pre-destruction callback. The former will be invoked after all resource injections have been performed on an instance of a managed class (eg servlet, filter or listener) but before it goes into service. The latter is invoked just before the container removes the instance from service. Let's look at how you might declare these callbacks in a web.xml file:

The above example would invoke the method myPostConstructCallback() on the instance of the class com.acme.MyServlet before the servlet is put into service, and would invoke the method myPreDestroyCallback() on the instance before it goes out of service.

How to Configure in Jetty

To use injections and callbacks declared in web.xml, you simply follow the instructions for setting up JNDI. Once your webapp is configured for JNDI access (ie you have followed the instructions in the first section of the JNDI page), and you have defined the resources you want to access in a jetty configuration file (such as a jetty.xml or a WEB-INF/jetty-env.xml file as described on the JNDI page), you can reference them in your web.xml file.

The examples/test-jndi-webapp webapp in the Jetty distribution shows you how to go about this.

Resource Injections and Callbacks with Annotations

Introduction

The Servlet 2.5 Specification (JSR154) allows the use of annotations instead of/in addition to declarations in the web.xml file.

Let's see what the equivalent annotations would be for the examples we gave for the web.xml markup.

Here's the equivalent of the injection of the DataSource:

In this case, we've chosen to put the annotation on a setter method, but equally we could have chosen to put it onto a field instead. As you can see, at runtime, you can simply use the data member myDS to obtain a Connection because Jetty will have injected the field with the DataSource you configured named jdbc/mydatasource in a jetty.xml or WEB-INF/jetty-env.xml file.

Here's the equivalent for the EnvEntry:

Finally, here are the lifecycle callback equivalents:

Refer to the Common Annotations for the Java Platform Specification (JSR250) and the JavaEE Specification v5 (JSR244) for more extensive examples, and for precise rules on what source constructs can be marked with annotations.

How to Configure in Jetty

Firstly, read the information on setting up JNDI. Then apply the following configuration to instruct Jetty to search for annotations in the source code of your web application:

Once you've enabled annotation support for your webapp, and followed the instructions in JNDI to set up references to resources you want to access at runtime (eg org.mortbay.jetty.plus.naming.EnvEntry for environment values, org.mortbay.jetty.plus.naming.Resource for DataSources, Queues, Topics etc) you can go ahead a mark up your code.

  • No labels
Contact the core Jetty developers at www.webtide.com
private support for your internal/customer projects ... custom extensions and distributions ... versioned snapshots for indefinite support ... scalability guidance for your apps and Ajax/Comet projects ... development services from 1 day to full product delivery