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?
Use the @WebServlet annotation to define a servlet component in a web application. This annotation is specified on a class and contains metadata about the servlet being declared. The annotated servlet must specify at least one URL pattern. This is done by using the urlPatterns or value attribute on the annotation.
WebServlet Annotation AttributesString name - Name of the Servlet. String[] value - Array of URL patterns. String[] urlPatterns - Array of URL patterns to which this Filter applies.
A servlet is a Java programming language class that is used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With