Tuesday, April 7, 2020

Servlet @WebServlet Annotation

@WebServlet Annotation (javax.servlet.annotation)


       The @WebServlet annotation provides an alternative to the web.xml(deployment descriptor). However, there are still many reasons to keep the web.xml a web project. The @WebServlet annotation is specified in a Servlet class and keeps metadata about the Servlet that is being created. If a servlet component is declared with the @WebServlet annotation, the servlet container can identify this component at the loading time. However, we still need to extend the class HttpServlet to create a Servlet.

        It is not required to create a deployment descriptor (web.xml) file with @WebServlet annotated Servlets. The mapping of the servlet is written in Java code still web.xml can be used to map the Servlets. The web.xml can be used to contain the list of welcome pages, error pages, and providing filter order.

The @WebServlet annotation allows us to specify servlet-name, url-mapping, load on Start-up, Servlet-description, init-params, asyncSupported, etc


@WebServlet Annotation Attributes

  • String name-Servlet name
  • String description -Servlet Description, if available
  • String displayName -Servlet Display name, if available
  • String[] value -An array of URL patterns
  • Boolean asyncSupported - Asynchronous operation supported by our Servlet or not
  • String[] urlPatterns -An array of URL patterns upon which Filter is applicable
  • int loadOnStartup - The integer value provides us the startup ordering hint
  • WebInitParam[] initParams - Array of init parameters for our Servlet
  • String smallIcon - if available
  • String largeIcon - if available

@WebServlet Annotation Example


import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet( //<- @WebServlet Annotation
            //Attributes
            urlPatterns = {
                        "/AnnotationDemo",
                        "/WebServletAnnotation"
            },
            asyncSupported=true,
            displayName="@WebServletAnnotationDemo",
            description="This is an Exmaple of @WebServletDemo",
            initParams = {
                        @WebInitParam(name = "author_name", 
                                      value = "Pushpendra")
            })
public class AnnotationDemo extends HttpServlet {
      private static final long serialVersionUID = 1L;
      
    public AnnotationDemo() {
        super();

    }

      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            PrintWriter pout=response.getWriter();
            response.setContentType("text/html");
            ServletConfig config=getServletConfig();
            String author_name=config.getInitParameter("author_name");
            String servlet_name=config.getServletName();
            pout.append("<html><body><h3>Written by<br>"+author_name+"</h3><br>"+servlet_name+"</body></html>");
      }
}




Result:


 @WebServlet Annotation Example
@WebServlet Annotation Example