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.

Comments:

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

Posted by Thiago Souza on December 03, 2008 at 08:24 PM PST #

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?

Posted by Daniel F. Martins on December 03, 2008 at 08:25 PM PST #

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!

Posted by guest on December 03, 2008 at 10:52 PM PST #

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.

Posted by Shing Wai Chan on January 15, 2009 at 10:19 AM PST #

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?

Posted by Joonas Koivunen on January 15, 2009 at 04:11 PM PST #

Per discussion in Servlet 3.0 expert group, the servlet annotation names are updated as follows:
@WebServlet, @WebFilter, @WebListener and @WebInitParam.

Posted by Shing Wai Chan on May 04, 2009 at 09:40 AM PDT #

[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...

Posted by Arun Gupta's Blog on May 19, 2009 at 04:01 AM PDT #

[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...

Posted by Arun Gupta's Blog on May 20, 2009 at 11:16 AM PDT #

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

Posted by Shing Wai Chan on May 27, 2009 at 04:19 AM PDT #

Post a Comment:
Comments are closed for this entry.
About

Shing Wai Chan

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today