Akashic Records

Introduction to servlet 3 new features 본문

오래된글/Articles

Introduction to servlet 3 new features

Andrew's Akashic Records 2018. 4. 19. 14:10
728x90

Introduction to servlet 3.0 new features


The servlet 3.0 specification has come up with many new and exciting features. This article explains the major changes since the previous specification.


Web framework pluggability

Servlet API is the building block of the Java web applications. Almost all the java web application frameworks build on top of the Servlet API. Examples are Spring MVC, Struts, Struts 2, Stripes, and Rife etc. In order to hook this frameworks into your web application, you either declare a filter or a servlet into your web application and map URL patterns to that servlet/filter. There was no way that you can just put the jar file into web application lib directory and ready to go. 
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. Servlet 3.0 specification has made this thing easier through annotations and providing the way to programmatic define Filters, servlet, listeners and URL patterns.


Annotations

With servlet 3.0, web.xml deployment descriptor is totally optional. Servlets, Filters, Listeners, Init parameters can be defined using annotations. 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. 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.


@WebServlet

This annotation is used to define a Servlet component in a web application and defines the metadata about the servlet being declared. Read this tutorial on how to define servlet using @webServlet annotation 
Classes annotated with @WebServlet annotation must extend javax.servlet.http.HttpServlet class.


Example

@WebServlet(asyncSupported = true, name = "asyncServlet", urlPatterns = { "/async" })

public class AsyncServletExample extends HttpServlet {


@WebFilter

This annotation is used to define a filter in web application. 
The classes annotated with @WebFilter annotation must implement javax.servlet.Filter interface.


Example

@WebFilter(filterName="samplefilter", urlPatterns={"/foo/*", "/bar"})

public class SampleFilter implements Filter {


@WebInitParam

This annotation is used to specify any init parameters that must be passed to the Servlet or the Filter.


Example

@WebInitParam(name="param1", value="foo")


@WebListner

This annotation is used to define a listener. Any type of listener like ServletContexListner, ServletRequestListner or HttpSessionListner classes can be defined using this annotation.


Example

@WebListener()

public class SampleContextListner extends ServletContextListner {


@MultipartConfig

Servlet 3.0 specification comes with the built in file upload support. 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.

The @MultipartConfig 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.


File upload

Up to now Servlet API did not provide any built in support for handling file upload. we used various open source libraries like Commons file upload and COS multipart parser. 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


public Collection<Part> 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.


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 uses. However the top level element for the descriptor MUST be web-fragment and the corresponding descriptor file MUST be called web-fragment.xml


Asynchronous Servlets and comet support.

Servlet 3.0 makes developing Comet applications very easier. If you don’t know what is comet, read this Wikipedia article http://en.wikipedia.org/wiki/Comet_%28programming%29 
Some time 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.

It was just the basic introduction of the new features of servlet 3.0 specification. A lot more interesting stuff is yet to come. Next,in this series, I will write tutorials on each of the new features mentioned above.


728x90

'오래된글 > Articles' 카테고리의 다른 글

Stomp On Web Sockets  (0) 2018.04.19
HTML5  (0) 2018.04.19
SIP Servlet  (0) 2018.04.19
Introduction of JDK 7  (0) 2018.04.19
Inspecting HotSpot JVM Options  (0) 2018.04.19
Comments