63

In my application, I had a servlet which was defined like this in the web.xml:

<servlet>
    <display-name>Notification Servlet</display-name>
    <servlet-name>NotificationServlet</servlet-name>
    <servlet-class>com.XXX.servlet.NotificationServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>NotificationServlet</servlet-name>
    <url-pattern>/notification/*</url-pattern>
</servlet-mapping>

After moving to use Tomcat 7, I would like to use the @WebServlet annotation that will do the job.
Here is the way I did it:

@WebServlet( name="NotificationServlet", displayName="Notification Servlet", urlPatterns = {"/notification"}, loadOnStartup=1)
public class NotificationServlet extends HttpServlet {

And it does not work. Could someone please tell me what I did wrong?

HDJEMAI
  • 9,436
  • 46
  • 67
  • 93
Dvora
  • 1,175
  • 1
  • 16
  • 26
  • 11
    I was able to get rid of *so much* boilerplate XML because of this question. Hooray for annotations! – aroth Feb 19 '13 at 23:47

3 Answers3

114

Provided that you're sure that you're using Tomcat 7 or newer, the webapp's web.xml has to be declared conform Servlet 3.0 spec in order to get Tomcat to scan and process the annotations. Otherwise Tomcat will still run in a fallback modus matching the Servlet version in web.xml. The support for servlet API annotations was only added in Servlet 3.0 (Tomcat 7).

So, the root declaration of your web.xml must look like below (make sure you remove any DOCTYPE from web.xml too, otherwise it will still be interpreted as Servlet 2.3!).

<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">

Further, there's a minor difference in the URL pattern. The URL pattern /notifications will let the servlet only listen on requests on exactly that path. It does not kick in on requests with an extra path like /notifications/list or something. The URL pattern /notifications/* will let the servlet listen on requests with extra path info as well.

The minimum @WebServlet annotation should thus look like this

@WebServlet("/notifications/*")

The rest of attributes are optional and thus not mandatory to get the servlet to function equally.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    You may need to add ``. – Nicolas Zozol Nov 16 '13 at 22:49
  • 3
    One doesn't have to use `web.xml` at all when using `@WebServlet` annotation. It works without `web.xml`. – destan Jul 17 '14 at 10:28
  • 1
    @destan: that's correct. However, if you have one, as in OP's case, it must be at least 3.0. You eventually need one because not everything can be annotated. – BalusC Jul 17 '14 at 11:30
  • @BalusC but on servlet wiki http://en.wikipedia.org/wiki/Java_Servlet it showing support from version 2.5. Although I tested its not working with version 2.5 it requires version 3.0. But i am little bit confused after reading servlet wiki page – Aniket Sep 13 '14 at 10:27
  • 4
    @Aniket: So you say that information on Wikipedia is more reliable than the information in Oracle's own Servlet specification document? Why exactly? – BalusC Sep 13 '14 at 10:28
  • 1
    I had the strange effect that the servlet worked when it was just changed, compiled and redeployed to glassfish 4. When I recompiled again without changes, and deployed, it did not work. I added the metadata-complete="false" attribute and minimized the webservlet-annotation, and now it works. Other servlets were working the whole time though. – Herbert Sep 15 '14 at 07:52
6

One may also want to check for having two classes with an annotations with the same name:

@WebServlet(name = "Foo", urlPatterns = {"/foo"})
public class Foo extends HttpServlet {
    //...
}

And:

@WebServlet(name = "Foo", urlPatterns = {"/bar"})
public class Bar extends HttpServlet {
    //...
}

In this cases, one of the servlets will not work. If you don't use the name, leave it out, like @BalusC suggests. I got the strange behavior that one of the servlets only worked right after changing and compiling it, but not after compilation without changes.

Herbert
  • 5,279
  • 5
  • 44
  • 69
  • 1
    If you down vote, leave a comment, otherwise nobody will be able to respond to, correct or learn from it. Thank you. – Herbert May 29 '18 at 16:29
3

Additionally, in order to use these annotations and compile your code you must import the corresponding dependency in your pom.xml, but as provided cause your "Servlet 3.0" compliant server already has this.

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>