3. Annotation in Servlets 3.0 specification
• The new specification focuses on
- Ease of development
- Plugability and extensibility
- Asynchronous Support
- Security Enhancements
4. Ease of Development
• Annotation based declarative-style programming
• Use @WebServlet for developing Servlet
• Use @ServletFilter for developing Filter
• Use @WebServletContextListener for creating ServletContext
Listener
5. Annotation v/s Deployment Descriptor
• Interesting note:
- Deployment descriptor can be used along with annotation
- Deployment descriptor takes precedence over annotation
- Metadata-complete is a new attribute which contains either true or false
• True means deployment tool will ignore any servlet
annotation
• False means container must scan all the files for
annotation along with the deployment descriptor
8. Servlet Context Listener Annotation
• @WebServletContextListener
public class TestServletContextListener implements
javax.servlet.ServletContextListener {
....
public void contextInitialized(ServletContextEvent sce) {
....
}
public void contextDestroyed(ServletContextEvent sce) {
....
}
}
9. Servlet Filter Annotation
• @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() {
....
}
}
10. Summary
• Annotation support is available only from JDK 1.5 or above
• Annotation can be used along with tradition approach.
• Annotation makes development easier