Java Servlet Specification 3.0 – Overview and Quick Intro

Posted on August 9, 2010

1


Java Servlet Technology which is the widely accepted technology for creating dynamic java web applications and which is behind all the modern java web frameworks. This major release tries to address the most wanted featues from the community as a JSR and is approved in the form of JSR 315 and is planned to be part of JEE6 (JSR 316). The main focus for this release in helping in ease of developemnt using Java annotations, Pluggability and extendability of using fragments, Asynchoronos Servlet support and security enhancements.

Ease of Development

Using annoations Servlets join the band wagon of using declarative-style programming along with JPA, EJB3, Spring and the rest. This helpa developing Servlets, Filters more easily with the code and the related configuration in the same place and thus making the web.xml an optional. It’s the Servlet 3.0 supported contianer which will process all the classes at boot time to find out all the annotated servlets, filters and listener classes and made them available for serving. If you don’t want the container to process all the class files to figure out the annotated classes, you can turn it off using the new web.xml configuration parameter metadata-complete on web-app element to true. The other important addition which helps in ease of development and configuration is programmatic setup of serlvets and filters.

The new sample web.xml configuration

1: <web-app xmlns=”http://java.sun.com/xml/ns/javaee&#8221; xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221; schemalocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd&#8221; version=”3.0″>

2:   <display-name>Sample Web Application to test Servlet 3.0 </display-name>
3:  </web-app>

@WebServlet and @WebFilter are the two main annotations for configuring the servlets and filters in Servlet 3.0.

The urlPatterns attribute of the @WebServlet annotation qualifies the URL’s for this servlets to respond.

1: @WebServlet(name = “helloServlet”, urlPatterns = {“/hello”})

2:  public class HelloServlet extends HttpServlet {
3:  
4:  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
5:   PrintWriter writer = response.getWriter();
6:   writer.print("<h1>HttpServlet using Servlet 3.0</h1>");
7:  
8:  }
9:  }

Note that the class still needs to extends HttpServlet (It is a mandatory requirement)
Servlet filters and Listeners follow the same pattern except that the class needs to implement Filter and ServletContextListener classes and the annoation @WebFilter, @WebListener respectively

Asynchronous Support

Asynchronous Http Request Processing (aka Comet) is a way that allows you to process a single Http Request using non-blocking I/O. More details about Coment and Asynchronous programming here(http://en.wikipedia.org/wiki/Comet_%28programming%29). Basically all this means is your servlet can continue working on the request while waiting for some other resource to be released or some other operation to be done like a database update.
To enable async support for your servlet, you can enable it using asyncSupported attribute of @WebServlet to true. Enabling this will give you support for few new methods in the Servletrequest itself inclluding startAsync(servletRequest, servletResponse), startAsync(), and getAsyncContext(). Once async attribute is set on the Servlet class you should either call startAsync(servletRequest, servletResponse) or startAsync() method to make an asynchronous request.

A simple example:
1: @WebServlet(“/foo” asyncSupported=true)

 2:  public class MyServlet extends HttpServlet {
 3:   public void doGet(HttpServletRequest req, HttpServletResponse res) {
 4:       AsyncContext aCtx = request.startAsync(req, res);
 5:       ScheduledThreadPoolExecutor executor = new ThreadPoolExecutor(10);
 6:       executor.execute(new AsyncWebService(aCtx));
 7:   }
 8:  }
 9:  
10:  public class AsyncWebService implements Runnable {
11:   AsyncContext ctx;
12:   public AsyncWebService(AsyncContext ctx) {
13:       this.ctx = ctx;
14:   }
15:   public void run() {
16:       // Invoke web service and save result in request attribute
17:       // Dispatch the request to render the result to a JSP.
18:       ctx.dispatch("/render.jsp");
19:  }
20:  }

More detailed tutorial on writing Asynchronous Servlets in my next entry.

Pluggability

A new notion called web fragments has been introduced in servlet 3.0 specification which helps reduce the configuration. A webfragment is a part or all of web.xml configuration that can be specified by any framework or a jar file dropped into the web application. In this way the framework can by default configure itself to work with the application with out the application developer confioguring it.

1: <web-fragment>

 2:  <servlet>
 3:  <description></description>
 4:  <display-name>TestServlet</display-name>
 5:  <servlet-name>TestServlet</servlet-name>
 6:  <servlet-class>sample.TestServlet</servlet-class>
 7:  </servlet>
 8:  <servlet-mapping>
 9:  <servlet-name>TestServlet</servlet-name>
10:  <url-pattern>/TestServlet</url-pattern>
11:  </servlet-mapping>
12:  </web-fragment>
Advertisements