Tuesday, October 11, 2011

@WebServlet annotation

The Servlet 3.0 spec lets you use annotations to configure servlets etc. I just spent 30 minutes trying to figure out why the @WebServlet annotations were ignored by Tomcat 7. Turns out that in my web.xml I specified the wrong servlet spec, it should read:

<web-app id="WebApp_ID" version="3.0" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemalocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">

Notice the "3.0" and "3_0" in there? If instead you have "2.5" and/or "2_5" then Tomcat will adhere to the older spec and ignore the annotations.