Upgrades in Servlet 3.0

Servlet 3.0

After the release of Servlet 3.0 specification, ground breaking impact has been found on Java Web application development platforms.

Major upgrades in Servlet 3.0

  • Asynchronous Support
  • Annotations (ease of configurations)
  • File upload
  • Modularization of web.xml

Asynchronous Support in Servlet 3.0

Asynchronous support is Servlet 3.0′s most significant enhancement, intended to make the server-side processing of Ajax applications much more efficient.

After Web 2.0 and applications like Gmail, Meebo and Face book, Comet programming is now one of the corner stone for developing rich web applications, in which the Server push the information to the client (browser).

Servlet 3.0 provides asynchronous processing via two methods

  • startAsync()
  • startAsync(request, response)

The difference between the two method signatures is that the startAsync() with no parameters implicitly uses the original request and response while the startAsync(req, res) uses the request and response objects passed in. The request and response passed in can be wrapped by filters or other Servlets earlier in the request processing chain.


Click to read more about asynchronous Support in Servlet 3.0

Annotations in Servlet 3.0

Deployment descriptor (web.xml) is now optional; Servlets, Filters, Listeners or Init parameters are now easily defined using annotations.

Some annotations are

  1. @WebServlet

used to define a Servlet and its meta-data

1
2
3
4
5
6
7
8
9
@WebServlet(asyncSupported = false, name = "SyedAnnotationServlet",
 urlPatterns = {"/hellosyed"},
 
initParams = {@WebInitParam(name="param1", value="value1"),
@WebInitParam(name="param2", value="value2")}
 
)
 
public class SyedAnnotationServlet extends HttpServlet {

2.       @WebFilter

used to define a filter in web application.

1
2
@WebFilter(filterName="samplefilter", urlPatterns={"/foo/*", "/bar"})
public class SampleFilter implements Filter {

3.       @WebListner

used to define a listener (HttpSessionListener, Servlet RequestListener  or HttpSessionBindingListener or ServletContextListener)

1
2
@WebListener()
public class SampleContextListner extends ServletContextListner {

4.       @MultipartConfig

this annotation, when specified on a Servlet, indicates that the request it expects is of type mime/multipart. The HttpServletRequest object of the corresponding servlet makes available the mime attachments via the getParts and getPart methods to iterate over the various mime attachments.  This annotation can be used to specify the location where the files can be stored, maximum size of the file being uploaded, maximum request size and the size threshold after which the file will be written to the disk. There is any equivalent <multipart-config> element in web.xml that can be used for the same purpose.

Make sure to put your Servlets under WEB-INF/classes directory. If you are deploying a jar, make sure it is under WEB-INF/lib

File Upload support in Servlet 3.0

Web container itself can parse the multipart request and make mime attachments available through
HttpServletRequest object. @MultipartConfig(location=”c:\\tmp”, fileSizeThreshold=1024*1024, maxFileSize=1024*1024*5, maxRequestSize=1024*1024*5*5) Two new methods have been introduced to HttpServletRequest interface public Collection<Part> getParts() public Part getPart(String name). Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
                 resp.setContentType("text/html");
                PrintWriter out = resp.getWriter();
               Collection parts = req.getParts();
 
                out.write("
<h2> Total parts : "+parts.size()+"</h2>
");
 
                for(Part part : parts) {
                          printPart(part, out);
                          part.write("samplefile");
                 }
 
}

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.

Modularization of web.xml

Servlet 3.0 introduces notion of deployment descriptor fragments. Modularizing web.xml to allow frameworks / libraries to have their own entities defined and self contained within the framework.

The web.xml can be divided into parts called Web-fragments and bundled in jar file. The fragments must be located into META-INF directory of the jar file. One or more web fragments can constitute a single web.xml file

<webfragment>
 
<servlet> 
 
<servletname>name</servletname> 
 
<servletclass>…..DispatcherServlet </servletclass> 
 
<servlet> 
 
<listener>
 
<listenerclass>…..ContextLoaderListener </listenerclass> 
 
</listener>
 
<filter> </filter> 
 
</webfragment>

5 Responses to “Upgrades in Servlet 3.0”

  1. a small good intro for servlet 3.0 , worth reading specially before interview :)

  2. hermes handbags Says:

    Perhaps you’ve tried out twitterfeed for your blog site, it would be awesome.

  3. It feels very simple but it gives a lot of information on servlet 3.0.

  4. Marcelo Domingos Says:

    Bom resumo sobre os recursos oferecidos pela versão 3.0 do Servlet

  5. subhashini Says:

    does iplanet 7 supports servlet verison3 ?

Leave a Reply

Spam Protection by WP-SpamFree

Einfobuzz Site Map