Login | Register   
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Servlet 3.0: A Sneak Preview : Page 4

Find out which features in the upcoming Servlet 3.0 specification will affect a major change in the way developers build Java web applications.


advertisement

Asynchronous Processing in Servlet

In many situations, a Servlet has to interact with a resource for processing data. It could be a database resource, a web service, or a message resource. While interacting with these resources, the Servlet has to wait until it gets a response from the resource before it actually generates a response. This makes the Servlet call to the resource a blocking call and results in inefficiency. Servlet 3.0 addresses this issue with the introduction of asynchronous processing. Asynchronous processing allows the thread to issue a call to the resource and return back to the container without getting blocked. The thread can perform other tasks, which makes Servlet 3.0's performance more efficient. AsyncContext, which manages the response from the resource, decides whether the response from the resource will be handled by the same thread or dispatched to a new resource in the container. AsyncContext has methods such as start, dispatch, and complete to perform the asynchronous process.

Servlet 3.0's asynchronous processing is supported through the asyncSupported attribute available in both @WebServlet and @WebFilter annotations. This attribute takes a Boolean value, which by default has a false value. You enable asynchronous processing in Servlet by setting a true value to this attribute in the Servlet or in the filter.

When combined with comet, Servlet asynchronous processing becomes an ideal solution for AJAX applications. In the init method of a Servlet, the thread can start any database operation or read/write a message to a queue. In the doGet or doPost method, you can start the asynchronous processing and the AsyncContext will manage the thread related to the database or message operation with the help of AsyncEvent and AsyncListener.



Enhancements to Existing APIs

In addition to the introduction of new concepts/techniques, the Servlet 3.0 specification also made the following enhancements to existing APIs.

HttpServletRequest

To support the multipart/form-data MIME type, the following methods have been added to the HttpServletRequest interface:
  • Iterable<Part> getParts()
  • Part getPart(String name)

Cookies

To avoid certain types of cross-site scripting attacks, Servlet 3.0 supports HttpOnly cookies. HttpOnly cookies are not exposed to the client-side scripting code. The following methods are added to the Cookie class to support HttpOnly cookies:
  • void setHttpOnly(boolean isHttpOnly)
  • boolean isHttpOnly()

ServletContext

With the addition of the following methods to the ServletContext API, Servlet 3.0 allows Servlets and filters to be programmatically added to a context:
  • addServlet(String servletName, String className)
  • addServlet(String servletName, Servlet servlet)
  • addServlet(String servletName, Class<? extends Servlet> servletClass)
  • addFilter(String filterName, String className)
  • addFilter(String filterName, Filter filter)
  • addFilter(String filterName, Class<? extends Filter>filterClass)
  • setInitParameter (String name, String Value)—This method was added to set the context initialization parameter.

The Servlet 3.0 Specification Features

As you can see, the Servlet 3.0 specification offers a lot of interesting features, including ease of development through annotations, fewer configurations of frameworks through the introduction of web fragments, and enhanced response with the introduction of asynchronous processing. This major release is sure to attract a large developer community.

Authors' Acknowledgements: The authors would like to sincerely thank Mr. Subrahmanya SV (VP, ECOM Research Group, E&R) for his ideas, guidance, support, and constant encouragement, and Mr. Piramanayagam Manickam (Senior Technical Architect, ECOM Research Group, E&R) for kindly reviewing this article and providing valuable comments.



Sangeetha S. works as a technical architect at E-Commerce Research Labs at Infosys Technologies. She has more than 14 years of experience in the design and development of Java and Java EE applications. She has co-authored a book on J2EE Architecture and also has written articles for JavaWorld and java.net.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap