X

Technology insights, news and tips.

  • Sun
    December 2, 2008

Servlet 3.0 Annotations

The JSR 315: Java Servlet 3.0 Specification
expert group is in the process of making
Public Review available. You can look at Rajiv's blog for more details.
The reference implementation is available
in GlassFish v3 nightly build.
In Servlet 3.0, for ease of development, several new annotations are defined.
These annotations are resided in the package javax.servlet.annotation.
They are intended to provide meta data only. In other words, one still need
to extend the corresponding class or implement the corresponding interface.

Now, one can have Servlet, Filter
and ServletContextListener in a war file without web.xml.
In this blog, I will discuss the following annotations:

  • @WebServlet
  • @ServletFilter
  • @WebServletContextListener

Servlet Annotation ( @WebServlet )


In JSR 315, one can specify the servlet meta data by using @WebServlet.
For instance,


    @WebServlet(name="mytest",

        urlPatterns={"/myurl"},

        initParams={ @InitParam(name="n1", value="v1"), @InitParam(name="n2", value="v2") })

    public class TestServlet extends javax.servlet.http.HttpServlet {

        ....

    }

In this example, the class TestServlet is a servlet as it
extends HttpServlet.
The @WebServlet provides the following meta data:

  • the name of the servlet, mytest, corresponds to <servlet-name> under <servlet> in web.xml
  • the url pattern of the servlet, /myurl, corresponds to <url-pattern> under <servlet-mapping> in web.xml
  • initialization parameters of the servlet, n1=v1, n2=v2, corresponds to <init-param> under <servlet> in web.xml
      <init-param>
        <param-name>n1</param-name>
        <param-value>v1</param-value>
      </init-param>
      <init-param>
        <param-name>n2</param-name>
        <param-value>v2</param-value>
      </init-param>
    Note that in this case, @InitParam is used to specify the name/value pairs.

Servlet Filter Annotation ( @ServletFilter )


One can specify the servlet filter meta data by using @ServletFilter.
For instance,


    @ServletFilter(urlPatterns={"/myurl"}.

        initParams={ @InitParam(name="mesg", value="my filter") })

    public class TestFilter implements javax.servlet.Filter {

        ....

        public void init(FilterConfig filterConfig) throws ServletException {

            ....

        }



        public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

            ....

        }



        public void destroy() {

            ....

        }

    }

In this example, the class TestFilter is a servlet filter as it
implements Filter.
The @ServletFilter provides the following meta data:

  • the url pattern of the filter applied, /myurl
  • initialization parameter of the filter, mesg=my filter, corresponds to <init-param> under <filter> in web.xml
    Note that in this case, @InitParam is used to specify the name/value pairs.

Servlet Context Listener Annotation ( @WebServletContextListener )


One can specify the servlet content listener met data by using @WebServletContextListener. For instance,


    @WebServletContextListener

    public class TestServletContextListener implements javax.servlet.ServletContextListener {

        ....

        public void contextInitialized(ServletContextEvent sce) {

            ....

        }



        public void contextDestroyed(ServletContextEvent sce) {

            ....

        }

    }

In this example, the class TestServletContextListener is a servlet
context listener as it implements ServletContextListener.
The @WebServletContextListener provides the meta data that this is a servlet context listener in a given war file.

Join the discussion

Comments ( 9 )
  • Thiago Souza Thursday, December 4, 2008

    Please, correct me if I'm wrong, but servlet configuration is hardcoded in annotation parameters, WTF?!?!?!?!


  • Daniel F. Martins Thursday, December 4, 2008

    That's wonderful!

    I have a question regarding ServletFilters though. Let's say that I have two ServletFilters: A and B. It's possible to ensure that the filter A will be placed before filter B in the filter chain, right?


  • guest Thursday, December 4, 2008

    If I'm not mistaken, a vast majority of web MVC frameworks are based on servlet filters, (i.e. Struts, Stripes, Wicket, etc.) So instead of writing static configuration in the web.xml, a programmer could now just use the @ServletFilter annotation to specify the filter to use? I like it. This would make migration from one framework to another a lot easier IMHO. I can't wait to see more!


  • Shing Wai Chan Thursday, January 15, 2009

    Per discussion in Servlet 3.0 expert group, three of annotations have been renamed from @InitParam, @ServletFilter, @WebServletContextListener to @WebInitParam, @WebFilter, @WebContextListener respectively. The @WebServlet remains unchanged.


  • Joonas Koivunen Friday, January 16, 2009

    What is the main reason for annotations like @WebServletContextListener?

    If the class still implements an interface A without which it couldn't be used by the container, shouldn't the container just look for classes that implement interface A, attempt to instantiate them and use them?

    Annotations have their uses but I cannot see one here. Could someone explain this?


  • Shing Wai Chan Monday, May 4, 2009

    Per discussion in Servlet 3.0 expert group, the servlet annotation names are updated as follows:

    @WebServlet, @WebFilter, @WebListener and @WebInitParam.


  • Arun Gupta's Blog Tuesday, May 19, 2009
    [Trackback] EJB 3.1 (JSR 318) and Servlet 3.0 (JSR 315) are the two new JSRs in Java EE 6 (JSR 316). The EJB 3.1 specification provides multiple new features such as WAR packaging, Optional Local Business Interfaces, EJB.lite, Portable Global...
  • Arun Gupta's Blog Wednesday, May 20, 2009
    [Trackback] EJB 3.1 (JSR 318) and Servlet 3.0 (JSR 315) are the two new JSRs in Java EE 6 (JSR 316). The EJB 3.1 specification provides multiple new features such as WAR packaging, Optional Local Business Interfaces, EJB.lite, Portable Global...
  • Shing Wai Chan Wednesday, May 27, 2009

    In this moment, the above annotations are for specifying meta-data. So, one still need to implement the corresponding interfaces.


Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.