Servlet 3.0 Part – 1

June 16, 2011 Leave a comment

 

Deployment Description is Optional in Servlet 3.0

 

With Servlet3.0 Servlets, Filters, Listeners, Init parameters can be defined using annotations. Container will process these classes (classes using annotations) if they are placed under WEB-INF/classes or packaged in .jar file located WEB-INF/lib based upon the annotation.

 

@WebServlet annotation : First of all to define a annotated Servlet component with @WebServlet, a Servlet class must extend the javax.servlet.http.HttpServlet class. So, @WebServlet defines a Servlet in a web application and following are attributes of @WebServlet :

 

  1. value(default attribute) : To set url for which the sevlet should be invoked. Here are two attributes – value and urlPatterns for the same purpose. Use “value” attribute when the only attribute on the annotation is the url pattern.
    package com.adc;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    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("/hello")
    public class HelloServlet extends HttpServlet {
    
        @Override
        protected void doGet(HttpServletRequest req,
                             HttpServletResponse resp)
                          throws ServletException, IOException {
                 resp.setContentType("text/html");
                 PrintWriter pw=resp.getWriter();
                pw.println("Hello! From the Serlvet");
        }
    }
  2. urlPatterns: Array of url patterns for which the sevlet should be invoked. This attribute must be used when the other attributes of @WebServlet are also used. It is important to note that it is illegal to have both value and urlPatterns attribute used together on the same annotation.
    package com.adc;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.util.ArrayList;
    import java.util.Enumeration;
    import java.util.List;
    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(
                name = "HelloServlet",
                asyncSupported = false,
                urlPatterns = {"/hello","/hi"},
                initParams=
                 {
                   @WebInitParam(name="servlet_spec",value="3.0"),
                   @WebInitParam(name="app_name",value="Annotated Servlet")
                  }
               )
    
    public class FirstServlet extends HttpServlet {
    
        private List list=new ArrayList();
    
        @Override
        protected void doGet(HttpServletRequest req,
                       HttpServletResponse resp)
    throws ServletException, IOException {
             resp.setContentType("text/html");
             PrintWriter pw=resp.getWriter();
             pw.println("GET");
             for(String s:list)
                 pw.println(s);
        }
    
        @Override
        public void init(ServletConfig config) throws ServletException {
             Enumeration e=config.getInitParameterNames();
             while(e.hasMoreElements())
             {
                 String key=e.nextElement().toString();
                 String value=config.getInitParameter(key);
                 list.add(String.format("%s : %s", key,value));
             }
        }
    }
  3. name – Name of the servlet which is an optional.
  4. asyncSupported – Boolean value true/false – It specifies weather the servlet supports asynchronous processing or not.
  5. largeIcon – large icon for this Servlet, if present – optional
  6. smallIcon – small icon for this Servlet, if present – optional
  7. description – Servlet description – optional
  8. initParams – Array of @WebInitParam, used to pass servlet config parameters – optional
  9. loadOnStartup – Integer value – optional
  10. displayName -Servlet display name – optional
Categories: Java Tags: ,