The third version of Servlet api came with quite interesting new features -such as asynchronous processing- that simplified servlet/JSP development.In this tutorial we will see how to configure a servlet using Servlet 3.0 api annotations.

As we all know in order to declare a servlet we must add  at the web.xml an xml snippet like the following:

<servlet>          <servlet-name>aServlet</servlet-name>          <servlet-class>             com.javaonly.servlets.AServlet         </servlet-class>          <init-param>              <param-name>initParam</param-name>              <param-value>initParamValue</param-value>          </init-param>  </servlet>         <servlet-mapping>    <servlet-name>aServlet</servlet-name>          <url-pattern>/rest/*</url-pattern>      </servlet-mapping>

 In Servlet 3.0 api can be done with annotations in the servlet class.For example:  

@WebServlet(name="aServlet",
        urlPatterns={"/*"},
        asyncSupported=false,
        initParams={ @InitParam(name="initParam", value="initParamValue")})
    public class TestServlet extends javax.servlet.http.HttpServlet {
       
           public void doGet(HttpServletRequest request,HttpServletResponse response){
                 //do something usefull
           }
           //other methods go here
    } 

As you can see the whole class is annotated with @WebServlet annotation and  xml tags such as init-param and url-pattern are now properties of the @WebServlet annotation.Later,the annotated class will be processed by the servlet container and the servlet will become available in the declared url.

Note also the asycSupported property that enables asynchronous processing but this will be the subject of an other tutorial