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>inweb.xml - the url pattern of the servlet,
/myurl, corresponds to<url-pattern>under<servlet-mapping>inweb.xml - initialization parameters of the servlet,
n1=v1, n2=v2, corresponds to<init-param>under<servlet>inweb.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,@InitParamis 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>inweb.xml
Note that in this case,@InitParamis used to specify the name/value pairs.
Servlet Context Listener Annotation ( @WebServletContextListener )
One can specify the servlet content listener met data by using@WebServletContextListener/code>. 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.
Please, correct me if I'm wrong, but servlet configuration is hardcoded in annotation parameters, WTF?!?!?!?!
Posted by Thiago Souza on December 04, 2008 at 04:24 AM 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 04, 2008 at 04:25 AM 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 198.30.81.2 on December 04, 2008 at 06:52 AM 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 06:19 PM 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 16, 2009 at 12:11 AM 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 04:40 PM PDT #
Posted by Arun Gupta's Blog on May 19, 2009 at 11:01 AM PDT #
Posted by Arun Gupta's Blog on May 20, 2009 at 06:16 PM 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 11:19 AM PDT #