Skip Headers
Oracle® Fusion Middleware Developing Web Applications, Servlets, and JSPs for Oracle WebLogic Server
11g Release 1 (10.3.6)

Part Number E13712-05
Go to Documentation Home
Home
Go to Book List
Book List
Go to Table of Contents
Contents
Go to Master Index
Master Index
Go to Feedback page
Contact Us

Go to previous page
Previous
Go to next page
Next
PDF · Mobi · ePub

8 WebLogic Annotation for Web Components

The following sections describe how to annotate Web components.

Servlet Annotation and Dependency Injection

With Java EE metadata annotations, the standard web.xml deployment descriptor is now optional. The Servlet 2.5 specification (see http://www.oracle.com/technetwork/java/javaee/servlet/index.html) states annotations can be defined on certain Web components, such as servlets, filters, listeners, and tag handlers. The annotations are used to declare dependencies on external resources. The container will detect annotations on such components and inject necessary dependencies before the component's life cycle methods are invoked. Dependency Injection (DI) will only be done on certain components, as described in Web Component Classes That Support Annotations.

Annotation processing and DI will be performed on all Web applications that have the version set to 2.5. However, annotation processing is expensive and it can increase the deployment time for Web applications depending on the size of the included classes. Set the metadata-complete attribute to true in the web.xml descriptor if your Web application does not have any annotations and if you have the version set to 2.5 to avoid unnecessary scanning of the Web applications classes for annotations. Alternatively, you can turn off annotation processing and DI for all the Web applications by setting -Dweblogic.servlet.DIDisabled=true flag when starting WebLogic Server.

For more information about using Java EE annotations and dependency injection with WebLogic Server applications, see "Using Java EE Annotations and Dependency Injection" in Developing Applications for Oracle WebLogic Server. For detailed information about EJB-specific annotations for WebLogic Server Enterprise JavaBeans, see "Programming WebLogic Enterprise JavaBeans, Version 3.0 for Oracle WebLogic Server".

Web Component Classes That Support Annotations

This section describes the behavior of annotations and dependency injection (DI) of resources in a Java EE compliant Web container.

The Web container only processes annotations for the types of classes listed in Table 8-1.

Table 8-1 Web Components and Interfaces Supporting Annotations and Dependency Injection

Component Type Interfaces

Servlets

javax.servlet.Servlet

Filters

javax.servlet.Filter

Listeners

javax.servlet.ServletContextListener
javax.servlet.ServletContextAttributeListener
javax.servlet.ServletRequestListener
javax.servlet.ServletRequestAttributeListener
javax.servlet.http.HttpSessionListener
javax.servlet.http.HttpSessionAttributeListener

Tag handlers

javax.servlet.jsp.tagext.SimpleTag
javax.servlet.jsp.tagext.BodyTag

The Web container will not process annotations on classes like Java Beans and other helper classes. The Web container follows these steps to achieve DI:

  1. Annotation Processing—The Web container processes annotations during the Web application deployment phase. As annotations are processed, the container figures out the relevant entries in the descriptor that get affected by the annotation and update the descriptor tree. The Servlet 2.5 specification at http://www.oracle.com/technetwork/java/javaee/servlet/index.html indicates that all annotations can be declared in the descriptor by defining an injection target. The Web container updates the descriptor tree with the injection targets so that as deployment continues the java:comp/env tree is updated with the necessary entries.

  2. Dependency Injection (DI)—DI is done when instances are created (for the types listed in Table 8-1). For listeners and filters, this occurs during the deployment phase, and for servlets it can occur during the deployment or run time.

    Note:

    In any Web application component, if one DI fails, it will cause all subsequent DIs upon the same component to be ignored.

Annotations Supported By a Web Container

Table 8-2 lists all the annotations that must be supported by the Web container.

Table 8-2 List of Supported Annotations

@Annotation Specification Reference

DeclaresRoles

14.5.1

EJB

14.5.2

EJBs

14.5.3

PersistenceContext

14.5.5

PersistenceUnit

14.5.7

PersistenceUnits

14.5.8

PersistenceContexts

14.5.6

PostConstruct

14.5.9

PreDestroy

14.5.10

Resource

14.5.4

Resources

14.5.11

WebServiceRef

14.5.13

WebServiceRefs

14.5.14

RunAs

14.5.12


The Web container makes use of the Java EE container's annotation processing and dependency injection mechanisms to achieve this functionality.

The Servlet 2.5 specification states that the Web container should not process annotations when metadata-complete attributes are set to true in the web.xml descriptor. If annotations are properly defined and annotation processing succeeds and dependencies are properly injected, the annotated fields are initialized properly and annotated methods are invoked at the proper phase in the life cycle. If DI fails, these annotated fields will be null.

Note:

If multiple methods in a Web component class, such as a servlet, filter, and such, are annotated with PostConstruct or PreDestroy, then the Web component will fail to deploy such an application. Similarly, if an EJB component class, such as a session bean, is annotated with PostConstruct or PreDestroy, or an EJB interceptor is annotated with PostConstruct, PreDestroy, PostActivate, or PrePassivate, then the EJB component will also fail to deploy such an application.

Fault Detection and Recovery

Any failure during annotation processing will yield a deployment exception that will prevent deployment of the Web application. If a failure happens during DI, the container will log a warning message in the server logs indicating the reason for the failure. The annotated fields in the instance of the class will be null and any life cycle annotated methods will not be invoked in case of DI failure.

Limitations

The WebLogic servlet container supports annotations on Web components that are declared in the web.xml descriptor. Any listeners, filters or servlets registered dynamically via the weblogic.servlet.WeblogicServletContext method will not have their annotations processed and no DI will be done for such components.

Annotating Servlets

The WebLogic servlet container provides the @WLServlet annotation for servlets and the WLFilter annotation for filters that you develop in a Web application without having to declare them in a web.xml descriptor. The WebLogic servlet container also provides the WLInitParam annotation to specify the initial parameters for servlets and filters declared using the WLServlet and WLFilter annotations.

All the required metadata can be annotated in the servlet or filter and the container will detect them and update the descriptor tree so that the annotated servlet or filter is deployed.

WLServlet

You can annotate a servlet class with WLServlet annotation (weblogic.servlet.annotation.WLServlet). This annotation defines various attributes for declaring parameters for the servlet. All attributes on this annotation are optional.

Attributes

Table 8-3 Attributes of WLServlet Annotation

Name Description Data Type Required?

displayName

Display name for the servlet after deployment

String

No

description

Servlet description

String

No

icon

Icon location

String

No

name

Servlet name

String

No

initParams

Initialization parameters for the servlet

WLInitParam[]

No

loadOnStartup

Whether the servlet should load on startup

int

No

runAs

The run-as user for the servlet

String

No

mapping

The url-pattern for the servlet

String[]

No


Example 8-1 illustrates the usage of the annotation in a servlet class.

Example 8-1 WLServlet Annotation

@WLServlet (
   name = "FOO",
   runAs = "SuperUser"
   initParams = { @WLInitParam (name="one", value="1") }
   mapping = {"/foo/*"}
)
. . .

The WebLogic servlet container detects the annotation and installs this servlet for deployment. During the annotation processing phase of the Web applications deployment, the descriptor bean corresponding to web.xml descriptor is updated with the relevant entries corresponding to the annotation.

Example 8-2 shows how the descriptor bean looks after being updated.

Example 8-2 Updated web.xml Descriptor

<web-app>
. . .
   <servlet>
      <servlet-name>FOO</servlet-name>
      <servlet-class>my.TestServlet</servlet-class>
      <init-param>
         <param-name>one</param-name>
         <param-value>1</param-value>
      </init-param>
   </servlet>
   <servlet-mapping>
      <servlet-name>FOO</servlet-name>
      <url-pattern>/foo/*</url-pattern>
   </servlet-mapping>
. . .
</web-app>

Fault Detection And Recovery

Any error during the processing of this annotation will result in a deployment error with a proper message in the server logs.

WLFilter

You can annotate a filter class with WLFilter annotation (weblogic.servlet.annotation.WLFilter). This annotation defines various attributes for declaring parameters for the filter. All attributes on this annotation are optional.

Attributes

Table 8-4 Attributes of WLFilter Annotation

Name Description Data Type Required?

displayName

Display name for the filter after deployment

String

No

description

Filter description

String

No

icon

Icon location

String

No

name

Filter name

String

No

initParams

Initialization parameters for the filter

WLInitParam[]

No

mapping

The url-pattern for the filter

String[]

No


Example 8-3 illustrates the usage of the annotation in a filter class.

Example 8-3 WLFilter Annotation

@WLFilter (
   name = "BAR",
   initParams = { @WLInitParam (name="one", value="1") } 
   Mapping = {"/bar/*"}
)
. . .

The WebLogic servlet container detects the annotation and installs this filter for deployment. During the annotation processing phase of the Web application deployment, the descriptor bean corresponding to web.xml descriptor is updated with the relevant entries corresponding to the annotation.

Example 8-4 shows how the descriptor bean looks after being updated.

Example 8-4 Updated web.xml Descriptor

<web-app>
. . .
   <filter>
      <filter-name>BAR</filter-name>
      <filter-class>my.TestFilter</filter-class>
      <init-param>
         <param-name>one</param-name>
         <param-value>1</param-value>
      </init-param>
   </filter>
   <filter-mapping>
      <filter-name>BAR</filter-name>
      <url-pattern>/bar/*</url-pattern>
   </filter-mapping>
. . .
</web-app>

Fault Detection and Recovery

Any error during the processing of this annotation will result in a deployment error with a proper message in the server logs.

WLInitParam

You can use the @WLInitParam annotation (weblogic.servlet.annotation.WLInitParam) to specify the initial parameters for servlets and filters declared using the @WLServlet and @WLFilter annotations.

Attributes

Table 8-5 Attributes of WLFilter Annotation

Name Description Data Type Required?

name

The initial parameter name.

String

No

value

The initial parameter value.

String

No


Example 8-5 provides an example of WLInitParam annotation.

Example 8-5 Example WLInitParam Annotation

initParams = {@WLInitParam(name="one", value="1"),
               @WLInitParam(name="two", value="2")}

Annotating a servlet or filter class with the above annotation is equivalent to declaring the init params in Example 8-6 in the web.xml descriptor.

Example 8-6 Init Params In web.xml

. . .
<init-param>
  <param-name>one</param-name>
  <param-value>1</param-value>
</init-param>
<init-param>
  <param-name>two</param-name>
  <param-value>2</param-value>
</init-param>
. . .