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 6 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 injection 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.

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

TO DO

How to Configure in Jetty

  • 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