Developing Web Applications, Servlets, and JSPs for WebLogic Server

     Previous  Next    Open TOC in new window    View as PDF - New Window  Get Adobe Reader - New Window
Content starts here

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, 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 Annotations and Dependency Injection in Developing Applications With WebLogic Server. For detailed information about EJB-specific annotations for WebLogic Server Enterprise JavaBeans, see Programming WebLogic Enterprise JavaBeans, Version 3.0.

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 WebApp 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 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 runtime.
  3. Note: In any WebApp component, if one DI fails, then 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 make 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.

Caution: If multiple methods in a web component class, such as a servlet, filter, etc., 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 to develop servlets and filters 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 meta-data 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

Description

Target: Class

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

Listing 8-1 illustrates the usage of the annotation in a Servlet class.

Listing 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.

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

Listing 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

Listing 8-3 illustrates the usage of the annotation in a Filter class.

Listing 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 WebApp deployment, the descriptor bean corresponding to web.xml descriptor is updated with the relevant entries corresponding to the annotation.

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

Listing 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

Description

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

Listing 8-5 provides an example of WLInitParam annotation.

Listing 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 Listing 8-6 in the web.xml descriptor.

Listing 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>
. . .

  Back to Top       Previous  Next