Thursday, September 30, 2010

Introduction to servlet 3.0 new features

Servlet API is the building block of the Java Web Applications. Almost all the java web application frameworks build on top of the Java Servlet API. Servlet 3.0 is JSR 315 JavaTM Specification. The Servlet API 3.0 Sspecification has come up with many new and exciting features as follows.

1. Programmatic instantiation of Servlets/Filters/listeners
2. Ease of Development through Annotations
3. File upload
4. Modularization of web.xml
5. Asynchronous Servlets and comet support.
6. Pluggable Web framework

1. Programmatic instantiation Filters, servlets, listeners
Servlet 3.0 specification has additional methods to the ServletContext to register dynamically Web components like Servlets or Filters or even Roles.

2. Ease of Development through Annotations
The servlet 3.0, web.xml deployment descriptor is totally optional. Servlet 3.0 specification has made this thing easier through annotations and providing the way to programmatic defination of Filters, servlet, listeners and URL patterns. Deployment descriptor can be used to override the configuration.

For example, you can define a servlet using @WebServlet annotation and put into WEB-INF/classes directory or application's WEB-INF/lib directory. The servlet does not need to be defined into web.xml, at run time container will process classes in WEB-INF/classes and lib directory and identify it based on the annotation.

Note: Classes using annotation will have their annotation processed on if they are located in WEB-INF/classes directory or if they are packaged in a jar file located in WEB-INF/lib directory.

3. File upload support
Servlet API, previously did not provide any built in support for handling file upload. We used various open source/third party libraries like Commons file upload and other APIs. However supporting file upload is so common requirement for any web application that Servlet 3.0 Specification supports it out of the box. Web container itself can parse the multipart request and make mime attachments available through HttpServletRequest object.

Two new methods have been introduced to HttpServletRequest interface are
public Collection getParts()
public Part getPart(String name)

Each part provides access to the headers, content type related with it and also the content via the getInputStream method.

The HTML form must specify multipart/form-data as encoding type and Servlet should be either annotated with @MultiPartConfig or configured with element in deployment descriptor.

4. Modularization of web.xml
The annotations make the web.xml deployment descriptor optional, However web.xml can be used to override the configuration values defined by the annotations. Currently before Servlet 3.0 specification web.xml is a big fat file with all the configuration for entire application. However Servlet 3.0 introduces notion of deployment descriptor fragments. The web.xml can be divided into parts called fragments and bundled in jar file. The fragments must be located into META-INF directory of the jar file.

A web fragment is a logical partitioning of the web application in such a way that the frameworks being used within the web application can define all the artifacts without asking developers to edit or add information in the web.xml. It can include almost all the same elements that the web.xml descriptor does include. However the top level element for the descriptor MUST be web-fragment and the corresponding descriptor file MUST be called web-fragment.xml

5. Asynchronous Servlets and comet support
Servlet 3.0 makes developing Comet applications very easier. Sometime comet is referred as reverse Ajax also.
Servlet 3.0 added new methods into HttpServletRequest class to support the asynchronous request processing and response generation.

public AsyncContext startAsync(ServletRequest req, ServletResponse res) {}

This method puts the request into asynchronous mode and initializes it’s AsyncContext with the given request and response objects.

6.
Pluggable Web Framework support
Almost all the java web application frameworks build on top of the Servlet API. Examples are Spring MVC, Struts, Struts 2, JSF, Webwork, Stripes and the like. In order to hook this frameworks into your web application, you declare a filter or a servlet into your web application deployment descriptor and map URL patterns to that servlet/filter in addition to including it as a dependency in the WEB-INF/lib directory. There was no way that you can just put the jar file into web application lib directory and ready to go with the framework.

For example, when using Spring MVC, you have to declare DispatcherServlet in web.xml and map URLs to it, you cannot just put the Spring MVC jar into lib directory and start using it. Today in a servlet container the application has one monolithic web.xml that defines all the deployment artifacts for the application, including the dependencies of the application on the framework components.

To overcome this issue in Servlet 3.0 Specification, A framework can define it's own deployment descriptor that declares the necessary components for the framework that is then included for use in the web applicaton. At deployment the container is responsible for discovering the web.xml fragments and processing them. A new element is being added to the descriptor schema for web applications - web-fragment that can define servlets, filters and listeners. The above fragment would be included in the META-INF directory of the framework jar file. However the top level element for the descriptor MUST be web-fragment and the corresponding descriptor file MUST be called web-fragment.xml.

Hope this will help.

No comments:

Post a Comment