When and How to Use Servlet Annotations


In this article, I will explain When and How to Use Servlet Annotations. In fact, the Servlet version 3.0 introduces the concept of annotations. As a result, it is no longer required to use only the web.xml file for configuring the servlets. Hence, we have now annotations to configure various components of the servlet. Accordingly, IDEs like Eclipse have the support of using annotations.

Further, everything that can be done using annotations can also be done using we.xml. However, you can define a configuration parameter using web.xml as well as using annotations. When it happens, the definitions specified in web.xml will override the annotations.

How to Use Servlet Annotations

Basically, we use annotations just before defining the servlet. The following example shows that a servlet annotation is specified by providing the annotation followed by its attributes within parenthesis. Further, the attributes may specify name-value pairs.

Using Servlet Annotations
Using Servlet Annotations

Example of Servlet Annotations

The following example shows how to use annotations with a servlet. As can be seen in the code, we have used the @WebServlet and @WebInitParams annotations. While, the first annotation is used to specify the name of a servlet, the second one we have used to specify the initialization parameters.

import java.util.Enumeration;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.annotation.*;
import javax.servlet.*;
import java.util.*;
@WebServlet(value="/WelcomeServlet", initParams={@WebInitParam(name="x", value="34"), @WebInitParam(name="y", value="6.9"), @WebInitParam(name="z", value="45")})
public class WelcomeServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
     public WelcomeServlet() {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		int x, y;
		double p, q;
		ServletConfig fg=getServletConfig();

		Enumeration<String> pm=fg.getInitParameterNames();
		String s2="";
			String s3=pm.nextElement();
			s2+=s3+" = "+fg.getInitParameter(s3)+"<br/>";
		PrintWriter ob=response.getWriter();
		ob.println("<center><h2>Welcome to Servlet Programming!!!");
		ob.println("<h3>Initialization Parameters...<br>");
		ob.print("<br>Computed Value by multiplying above three parameters: "+q+"</h3>");

As can be seen in the above code, the three initialization parameters are specified using separate @WebInitParam annotation. Moreover, we use it as a part of the @WebServlet annotation. Moreover, we can also specify a URL pattern using @WebServlet annotation.


Example of Using Servlet Annotations
Example of Using Servlet Annotations

When Should We Use Servlet Annotations?

Once, your dynamic web project becomes so large that it includes a good number of servlets, and JSP files, the corresponding deployment descriptor is also populated with hundreds of entries. When it happens it really becomes very cumbersome to find and update the entry for a particular servlet. When it happens, it is desirable to use annotations rather than using web.xml for configuring servlets.

Besides these two annotations, there are many more that we can use with servlets. For instance, we can use @WebFilter for a filter declaration. While @WebListener annotation declares a servlet listener. Also, we can use annotations to specify security constraints. For this purpose, we can use @ServletSecurity and @HttpConstraint.


You may also like...