Skip to main content

Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

Difference between revisions of "Index.php?title=Jetty/Feature/Annotations"

(New page: {{Jetty Feature |introduction = The 2.5 Servlet Specification adds the ability to inject JNDI resources into fields and methods of servlets, filters and listeners, ...)
 
(Removing all content from page)
 
Line 1: Line 1:
{{Jetty Feature
 
  
|introduction =
 
The 2.5 Servlet Specification adds the ability to inject [[Jetty/Feature/JNDI|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:
 
 
<source lang="xml">
 
 
<web-app
 
  xmlns="http://java.sun.com/xml/ns/j2ee"
 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 
  xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
 
  metadata-complete="true"
 
  version="2.5">
 
</source>
 
 
|body =
 
 
== 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 [[Jetty/Feature/JNDI|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 [[Jetty/Feature/JNDI|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 [http://jcp.org/en/jsr/detail?id=250 Common Annotations for the Java Platform Specification (JSR250)], the [http://jcp.org/en/jsr/detail?id=154 Servlet 2.5 Specification (JSR154)] and the [http://jcp.org/en/jsr/detail?id=244 JavaEE Specification v5 (JSR244)].
 
 
Here is a small example of the injection of a DataSource resource via a web.xml declaration:
 
 
<source lang="xml">
 
  <resource-ref>
 
    <res-ref-name>jdbc/mydatasource</res-ref-name>
 
    <res-type>javax.sql.DataSource</res-type>
 
    <res-auth>Container</res-auth>
 
    <injection-target>
 
      <injection-target-class>com.acme.MyServlet</injection-target-class>
 
      <injection-target-name>myDatasource</injection-target-name>
 
    </injection-target>
 
  </resource-ref>
 
</source>
 
 
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.
 
 
 
}}
 

Latest revision as of 00:19, 9 June 2010

Back to the top