Ever since Java introduced annotations, every framework has been looking to provide an XML alternative to their configuration information. So how could servlets be left behind.
So I decided to build a servlet application using annotations. In fact I decided to be a little more ambitious. Build a servlet web app without the deployment descriptor.
I created a servlet, a filter and a listener:
The filter properties that we specified in web.xml can be replaced by using the WebFilter annotation. The WebInitParam annotation allows us to define init parameters for the filter.
Next is the Servlet:
Last is the Listener:
However there are certain things that cannot be achieved using annotations:
So I decided to build a servlet application using annotations. In fact I decided to be a little more ambitious. Build a servlet web app without the deployment descriptor.
I created a servlet, a filter and a listener:
@WebFilter(description = "testFilter", displayName = "testFilter", filterName="SampleFilter",You still need to extend the Filter interface.
urlPatterns = { "*.do", "/htm/" },
initParams = @WebInitParam(name = "param1", value = "val1"),
dispatcherTypes = { DispatcherType.FORWARD, DispatcherType.REQUEST}
)
publicclass TestFilter implements Filter {
@Override
publicvoid init(FilterConfig filterConfig) throws ServletException {
System.out.println("TestFilter initialized as " + filterConfig.getFilterName()
+ " with init parameter value "
+ filterConfig.getInitParameter("param1"));
}
@Override
publicvoid doFilter(ServletRequest servletRequest, ServletResponse servletResponse,
FilterChain filterChain) throws IOException, ServletException {
System.out.println("request received in TestFilter");
filterChain.doFilter(servletRequest, servletResponse);
return;
}
@Override
publicvoid destroy() {
System.out.println("LocalFilter destroyed");
}
}
The filter properties that we specified in web.xml can be replaced by using the WebFilter annotation. The WebInitParam annotation allows us to define init parameters for the filter.
Next is the Servlet:
@SuppressWarnings("serial")Just like the Filter, the Servlet attributes can also be defined using annotations.
@WebServlet(displayName = "TestServlet", description = "Test Servlet",
loadOnStartup = 1, urlPatterns = {"*.do" },
//same as value
initParams = {
@WebInitParam(name = "param1", value = "val1"),
@WebInitParam(name = "param2", value = "val2") }
)
publicclass TestServlet extends HttpServlet {
@Override
publicvoid init(ServletConfig servletConfig) throws ServletException {
super.init(servletConfig);
System.out.println("TestServlet initialized as " + servletConfig.getServletName()
+ " with init parameter values " + servletConfig.getInitParameter("param1") + " and "
+ servletConfig.getInitParameter("param2"));
}
@Override
protectedvoid doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.getOutputStream()
.write("<html><body>Request received</body></html>".getBytes());
}
}
Last is the Listener:
@WebListener(value = "This is used to monitor Servlet Lifecycle events")The Listener annotation simply identifies the class as a listener. Based on the class hierarchy, the Listener type is identified. We can register the below Listener types:
publicclass TestListener implements ServletContextListener {
publicvoid contextInitialized(ServletContextEvent event) {
System.out
.println("ServletContext " + event.getServletContext().getServletContextName()
+ " has been initialized at " + new Date());
}
publicvoid contextDestroyed(ServletContextEvent event) {
System.out
.println("ServletContext " + event.getServletContext().getServletContextName()
+ " has been destroyed at " + new Date());
}
}
- Context Listener
- Context Attribute Listener
- Servlet Request Listener
- Servlet Request Attribute Listener
- Http Session Listener
- Http Session Attribute Listener
However there are certain things that cannot be achieved using annotations:
- Defining the welcome pages
- Defining the error page
- Providing display name, description for the web application.
- Defining context parameters
- Session Configuration Information - e.g. default session timeout
- Configurations for jsp files.
- Mime mappings ( wonder if they are used anymore)