17 - What’s new in servlet 3.0

17.1 Overview

JSR 315 (Servlet 3.0) is an update to the existing Servlet 2.5 specification. In previous chapters we have used Servlet 2.5 and in this chapter we will discuss the new features in Servlet 3.0.

17.2 New Features and changes in Servlet 3.0

17.2.1 Annotations 

One of the important and major change is to minimize the use of web.xml. Instead of configuring the servlets ,filters etc in web.xml file , annotations are introduced. Still there are certain configurations like welcome file lists, context params will still be configured in web.xml.

 Annotations makes the coding of servlet, filter, and listener classes easier, but also makes the deployment descriptors optional for a web application.

Annotations like @WebServlet, @WebFilter, @WebInitParam, @WebListeners , @MultipartConfig are added.

Refer Chapter 18 for annotation details.

17.2.2 Asynchronous Support

 In previous chapter specially MVC , we discussed about the business logic which is being invoked by servlets. In all of our examples, business logic has been  simple  but in actual scenarios they are very complex and may involve the interaction with other systems like Databases or invoking web services.

All of the business invocation is “Synchronous” which means Servlet will wait till the response is returned by business classes and this may results in a poor performance.

To overcome this issue, Asynchronous support has been introduced with Servlet 3.0. With this feature, Servlets can invoke Services asynchronously. When a request is suspended, the thread  handling the request will return to the container without generating any response and gets ready to perform other tasks. The resume method on the request resumes the request processing. Whenever the requested resource becomes available, the
thread handling that event resumes the suspended request and proceeds to generate the response.

New methods have been added in ServletRequest and ServletResponse to support this feature.

17.3.3 Web Fragments

Now a days , several frameworks are available like Spring, Struts, JSF etc and they all are built on top of servlets API. These frameworks provide several features out of the box like MVC, transactions etc. and require certain configurations to be enabled like Controllers in the web application.

We all know that configurations are done in web.xml and any unwanted changes in web,xml can break entire application or may start undesirably. If we think of a large web application, web.xml will have a lot of configurations and modifying it will not be an easy task.

Even to integrate any of other frameworks will require a change in web.xml. With the introduction of web-fragments, we can divide an application into modules and can package them into separate JARs files and include these archives into the main application. Each module can have its own deployment descriptor (called as fragment) contained within the META-INF folder of the JAR.  

With this feature we can have integrate third party functionality without changing web.xml

Refer Chapter 19 for examples

Like us on Facebook