18.1 Overview
As we discussed in earlier chapter that Servlets 3.0 has come up with a set of new Annotations for the Servlets, Init-Params, Listeners, and Filters that makes the use of Deployment Descriptor (web.xml) absolutely optional.
The Annotation based configurations makes the code more readable and also saves us from maintaining deployment descriptors.
In this chapter we will discuss the important and most commonly used annotations.
18.2 Annotation Details
18.2.1 @WebServlet -
@WebServlet annotation is the replacement of servlet configuration in web.xml. When we annotate our servlet class with @WebServlet annotation the container will be able to recognize this as a servlet at the loading time.
Class annotated with @WebServlet still needs to extends the HttpServlet class
With this annotation we can specify servlet-name, url-mapping, load on Start up, description ,init params ,async supported etc
Lets take an example Welcome Servlet which is defined in an web.xml like below
<servlet> <description>Welcome Servlet</description> <servlet-name>WelcomeServlet</servlet-name> <servlet-class>com.servlet.tutorial.WelcomeServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>WelcomeServlet</servlet-name> <url-pattern>/WelcomeServlet</url-pattern> </servlet-mapping>
Same servlet configuration can be done with @WebServlet annotation as shown below. @WebServlet annotation is highlighted below and all the attributes configured in web,xml are configured in annotation itself. With this approach, we need not to configure any entry in web.xml for Welcome Servlet.
- name- defines the name of servlet
- urlPatterns – maps the servlet to the pattern
- loadOnStartup – defines the value of load on start up
- description –description about servlet
- initParams – takes multiple @WebInitParam annotation
package com.servlet.tutorial; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name="/WelcomeServlet",urlPatterns="/WelcomeServlet",loadOnStartup=1, description="Welcome Servlet") public class WelcomeServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
Did you notice the word “urlPatterns”? it is not a pattern , it is patterns which means we can map a servlet with multiple URLs like
@WebServlet(name="/WelcomeServlet",urlPatterns={"/WelcomeServlet","/HelloServlet"},loadOnStartup=1, description="Welcome Servlet")
18.2.2 @WebInitParam
This annotation is used for init-param configurations of servlet. Remember we can define multiple init parameters for a servlet which we can get in the servlet using servlet config object.
@WebInitParam enables us to configure one init param and provides name , value and description attribute.
- Name – name of init param
- Value – value of init param
- Description –description of init param
One @WebInitParam annotation is needed for one init param tag and all @WebInitParam annotation are configured in initParams attribute of @WebServlet annotation.
Lets modify above Welcome Servlet’s web.xml to add two init params like below
<servlet> <description>Welcome Servlet</description> <servlet-name>WelcomeServlet</servlet-name> <servlet-class>com.servlet.tutorial.WelcomeServlet</servlet-class> <init-param> <description> param1 </description> <param-name>Param1</param-name> <param-value>Param 1 Value </param-value> </init-param> <init-param> <description> param2 </description> <param-name>Param1</param-name> <param-value>Param 2 Value</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>WelcomeServlet</servlet-name> <url-pattern>/WelcomeServlet</url-pattern> </servlet-mapping>
Correspondingly WelcomeServlet code will be modified like below
package com.servlet.tutorial; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; @WebServlet(name="/WelcomeServlet",urlPatterns="/WelcomeServlet",loadOnStartup=1, description="Welcome Servlet", , initParams={@WebInitParam(name="Param1",value="Param 1 Value",description="param1"),@WebInitParam(name="Param2",value="Param 2 Value",description="param 2")}) public class WelcomeServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
18.2. 3 @WebFilter
@WebFilter annotation is the replacement of filter configuration in web.xml. When we annotate our filter class with @WebFilter annotation the container will be able to recognize this as a filter at the loading time.
Class annotated with @WebFilter still needs to implements the Filter interface
With this annotation we can specify filter-name, url-mapping, description ,init params ,async supported etc
Lets take an example MyFilter which is defined in an web.xml like below
<filter> <filter-name>MyFilter</filter-name> <filter-class>com.servlet.tutorial.MyFilter</filter-class> <init-param> <description>param 1 description</description> <param-name>param1</param-name> <param-value>value 1</param-value> </init-param> <init-param> <description>param 2 description</description> <param-name>param2</param-name> <param-value>value 2</param-value> </init-param> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
Same filter configuration can be done with @WebFilter annotation as shown below. @WebFilter annotation is highlighted below and all the attributes configured in web,xml are configured in annotation itself. With this approach , we need not to configure any entry in web.xml for My Filter.
- filterName- defines the name of filter
- urlPatterns – maps the servlet to the pattern
- description –description about servlet
- initParams – takes multiple @WebInitParam annotation
package com.servlet.tutorial; import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.annotation.WebInitParam; @WebFilter( filterName="MyFilter", urlPatterns="/*", description="Filter description", initParams={ @WebInitParam(name="param1",value="value 1",description=" param 1 description "), @WebInitParam(name="param2", value="value 2", description=" param 2 description ") }) public class MyFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { System.out.println("Filter initialized..."); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { System.out.println("Filter executing..."); filterChain.doFilter(request, response); } @Override public void destroy() { System.out.println("Filter Destroyed.."); } }
As you can see attributes of @WebFilter are very much similar to @WebServlet
18.2.4 @MultipartConfig
With Servlet 3.0 , there is an inbuilt support for File Upload.When this annotation is added on the servlet, container knows that the Request this servlet is expecting will have multipart/form-data MIME data. This annotation can specify location to store the file on server, maxFileSize allowed for uploaded files, maxRequestSize allowed for multipart/form-data requets, and fileSizeThreshold after exceeding it the file content will be written on the disk.
18.2.5 @WebListener
@WebListener annotation is the replacement of listener configuration in web.xml. When we annotate our listener class with @WebListener annotation the container will be able to recognize this as a listener at the loading time. The class still needs to extend the appropriate listener class.
Lets take an example MyServletContextListener which is defined in an web.xml like below
<listener> <description>Servlet Context Listener Example</description> <listener-class>com.servlet.tutorial.MyServletContextListener</listener-class> </listener>
Same listener can be configured using @WebListener (refer below code)
18.3 Conclusion
In this chapter we have discussed about the annotation introduced in Servlet 3.0. In next chapter we will write examples using all these annotation and some examples for the new features discussed in chapter 17 so stay tuned.