Creating and Initializing a Servlet using Annotation

In this tutorial you will learn how a servlet can be created using Annotation

Creating and Initializing a Servlet using Annotation

In this tutorial you will learn how a servlet can be created using Annotation

Creating and Initializing a Servlet using Annotation

Creating and Initializing a Servlet using Annotation

In this tutorial you will learn how a servlet can be created using Annotation

To create a servlet using annotation you are required to define @WebServlet annotation. @WebServlet annotation holds metadata of the servlet is to be declared. The servlet which is annotated must be defined at least one URL pattern using urlPatterns or the value attribute. When there is only URL pattern attributes on the annotation then use the value attribute or when there are other attributes are also used then use the urlPatterns attribute. It should be noted that when you are using the @WebServlet with your class you must have to extend the javax.servlet.http.HttpServlet class. Use of Annotation reduces the explicitly entries of configuration info in web.xml in several cases but somewhere you will be required to give the configuration info in web.xml file. for example :

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.annotation.WebInitParam; 
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "MyAnnotationServlet", urlPatterns = {"/hello"},
initParams = {@WebInitParam(name="param1", value="value1"), @WebInitParam(name="param2", value="value2")}
)
public class MyAnnotationServlet extends HttpServlet
 {
public void doGet(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException
 {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h2>Servlet Annotation </h2>");
out.println("<br/>");
out.println("Init parameters are :");
out.println("<br/>");
out.println("<ol>");
out.println("
  • "+("param1="+getServletConfig().getInitParameter("param1"))+"
  • ");; out.println("
  • "+("param2="+getServletConfig().getInitParameter("param2"))+"
  • ");; out.println("</ol>"); out.close(); } }

    web.xml

    Note : It is not need to give configuration info explicitly.

    <servlet>
    <servlet-name>MyAnnotationServlet</servlet-name>
    <servlet-class>MyAnnotationServlet</servlet-class>
    
    <init-param> 
        <param-name>param1</param-name> 
        <param-value>value1</param-value> 
      </init-param> 
      <init-param> 
        <param-name>param2</param-name> 
        <param-value>value2</param-value> 
      </init-param> 
    
    </servlet>
    
    <servlet-mapping>
    <servlet-name>MyAnnotationServlet</servlet-name>
    <url-pattern>/hello</url-pattern>
    </servlet-mapping>
    

    Following attributes are supported by the @WebServlet annotation :

    • asyncSupported= defines that a servlet is asynchronously processing or not specified by the value true/false
    • name= It is an optional attribute that specifies the servlet name
    • initParams= It is an optional attribute that passes the servlet config parameters.
    • urlPatterns= It is an array of url patterns for which the servlet should be invoked.
    • displayName= It is an optional attribute that displays a servlet name.
    • loadOnStartUp= It is an optional attribute that requires an integer value.
    • description= It is an optional attribute that describes the servlet, etc.