Monday, April 30, 2012

New Features in Servlets 3.0


Introduction

This article covers most of the important features available as part of Servlet 3.0 specification. Note that the Servlet 3.0 specification constantly keeps changing frequently with the reviews coming in and the features and the API’s mentioned in this article is based on the specification that is available in JCP for public review as of December 2008. This article focuses on the new set of annotations introduced that can be used by developers rather than put the data in the configuration file, followed by the enhanced Pluggability and the extension support for adding third-party frameworks. The article is finally concluded by detailing about the asynchronous execution of processing and the usage for the same. JSR 315 talks about the Servlet 3.0 features. For attitional information please read the reference section of this article.

Ease of Development through Annotations

The configuration information about a component in a typical web application is expressed in an external meta file. The information about web components such as Servlets, Servlet Filters are mentioned in the deployment descriptor, which isweb.xml. Starting from Servlet 3.0, it is also possible to specify the meta information about a component in the definition of a component itself, through Annotations. It doesn’t mean the deployment descriptor is now gone, deployment descriptor in the form of web.xml is still there. In fact information specified in the deployment descriptor takes precedence over the information specified through Annotations.
The Servlet 3.0 specification also provides an option for instructing the Web Container, whether the container should process the annotations defined on the web components. The name of the element is metadata-complete and it is a child element of web-app element. The metadata-complete element indicates whether the meta-data information available in the deployment descriptor is complete. So, if the value for the metadata-complete element is set to a value of true, then it means that the meta information found in the deployment descriptor is complete and eventually the annotations defined on the web components will be ignored by the Servlet Container. If the value for metadata-complete is set to false, then it means that the information in the deployment descriptor is not complete and web components decorated with annotations, if any, should be scanned and processed by the Web Container.
The following annotations are applicable starting from Servlet 3.0 specification,
  • @WebServlet
  • @WebServletContextListener
  • @ServletFilter
  • @InitParam

Web Servlet and InitParam Annotation

In this section, we will see the usage of @WebServlet and @InitParam using an example. Look at the following code,
SimpleServlet.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package net.javabeat.servlet30.newfeatures;
 
import javax.servlet.annotation.InitParam;
import javax.servlet.annotation.WebServlet;
 
@WebServlet(
    name = "SimpleServlet", 
    urlPatterns = {"/simple"}, 
    initParams = {
        @InitParam(name = "param1", value = "value1"),
        @InitParam(name = "param2", value = "value2")}
)
public class SimpleServlet {
 
}
In the example, we have declared a class by name SimpleServlet and this class is not made to extend or implement any of the Servlet/HttpServlet types. Instead, to qualify this class as a Servlet class we have annotated using @WebServlet annotation. Note that the name of the servlet is SimpleServlet as specified through the name attribute. The attribute urlPatterns defines a set of url-patterns that can be used to invoke the Servlet. The Servlet Container after scanning this class will generate the deployment descriptor which may look like the following,
web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
...
 
<servlet>
    <servlet-name>SimpleServlet</servlet-name>
    <servlet-class>net.javabeat.servlet30.newfeatures.SimpleServlet</servlet-class>
 
        <init-param>
            <param-name>param1</param-name>
            <param-value>value1</param-value>
        </init-param>
 
        <init-param>
            <param-name>param2</param-name>
            <param-value>value2</param-value>
    </init-param>
 
</servlet>
 
<servlet-mapping>
    <url-pattern>/simple</url-pattern>
    <servlet-name>SimpleServlet</servlet-name>
</servlet-mapping>
 
...

Filter annotation

The @Filter annotation defines a Servlet Filter component for a web application. A filter is typically used to intercept a web request for performing any of the pre-processing operations well before reaching the actual servlet component. Let us see the definition of a filter component using Servlet 3.0 approach,
SimpleFilter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package net.javabeat.servlet30.newfeatures;
 
import javax.servlet.annotation.InitParam;
import javax.servlet.annotation.ServletFilter;
 
@ServletFilter(
    filterName = "SimpleFilter",
    urlPatterns = "/simple",
    initParams = 
        {@InitParam(name = "param1", value = "value1"),
         @InitParam(name = "param2", value = "value2")}
)
public class SimpleFilter {
 
}
Again, to support backward compatibility, the annotation information in the above class will be transformed as information in the deployment descriptor by the Servlet Container and the deployment descriptor might look like this,
web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
...
 
<filter>
 
    <filter-name>SimpleFilter</filter-name>
    <filter-class>net.javabeat.servlet30.newfeatures.SimpleFilter</filter-class>
 
    <init-param>
        <param-name>param1</param-name>
        <param-value>value1</param-value>
    </init-param>
 
    <init-param>
        <param-name>param2</param-name>
        <param-value>value2</param-value>
    </init-param>
 
</filter>
 
<filter-mapping>
    <filter-name>SimpleFilter</filter-name>
    <url-pattern>/simple</url-pattern>
<filter-mapping>
 
...

Servlet Context Listener annotation

The Servlet Context Listener is used to receive events whenever the servlet context is created and destroyed by the Web Container. Let us see an usage of this annotation,
SimpleServletContextListener.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package net.javabeat.servlet30.newfeatures;
 
import javax.servlet.ServletContextEvent;
import javax.servlet.annotation.WebServletContextListener;
 
@WebServletContextListener()
public class SimpleServletContextListener {
 
    public void contextInitialized(ServletContextEvent event){
    }
 
    public void contextDestroyed(ServletContextEvent event){
    }
}
The deployment descriptor in this case would be,
web.xml
1
2
3
4
5
6
7
8
<web-app>
 
    <listener>
        <listener-class>net.javabeat.servlet30.newfeatures.SimpleServletContextListener
    </listener-class>
    </listener>
 
</web-app>

No comments:

Post a Comment