2012-01-12

Tip: Getting rid of web.xml using Servlet 3.0

EJB until 2.1 used a tremendous amount of XML configuration files. This has been reduced drastically with EJB 3.0 because it uses a lot of default that mostly fit well. Now this continued to the Servlet 3.0 standard. In the older days we first defined a servlet like this:

 public class KeyServiceServlet extends HttpServlet {  
   @Override  
   public void service(ServletRequest req, ServletResponse res)  
       throws ServletException, IOException {  

Now we need a WEB-INF/web.xml that first defines the Servlet and does a mapping to one or multiple URLs

 <?xml version="1.0" encoding="UTF-8"?>  
 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee  
            http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"  
   version="3.0" metadata-complete="true">  
   <display-name>Key Service</display-name>  
   <description>  
     Simple frontend for the test service  
   </description>  
   <servlet>  
     <servlet-name>KeyService</servlet-name>  
     <servlet-class>com.prodyna.demo.key.web.KeyServiceServlet</servlet-class>  
   </servlet>  
   <servlet-mapping>  
     <servlet-name>KeyService</servlet-name>  
     <url-pattern>/*</url-pattern>  
   </servlet-mapping>  
 </web-app>  


With Servlet 3.0 we now can use annotations to do the same. We simply take the @WebServlet annotation

 @WebServlet(urlPatterns = { "/*" })  
 public class KeyServiceServlet extends HttpServlet {  
   @Override  
   public void service(ServletRequest req, ServletResponse res)  
       throws ServletException, IOException {  

I personally love the approach of removing unneeded XML and this an example where it really makes sense.