There are two ways for servlet mapping. The first is in web.xml:
<servlet>
<servlet-name>foo</servlet-name>
<servlet-class>com.whatever.foo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>foo</servlet-name>
<url-pattern>/foo</url-pattern>
</servlet-mapping>
The second method uses the WebServlet annotation:
@WebServlet("/foo")
public class foo extends HttpServlet {
...
}
Which one is better? Where are the advantages of the first and the second way?
XML configuration :
advantages :
All mappings are in the same location, you have an overview of all of them in one single file.
disadvantages :
Needs a separate file in addition to the class files.
Annotation configuration :
advantages :
The mapping is described directly inside the relevant class.
disadvantages :
You have to open a particular class to see its mappings.
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.
What is benefit of using java based config instead of web.xml for servlet 3.x?
It avoids repeating yourself, and making mistakes by doing so. The servlet class is, for example, com.foo.bar.SomeServlet
. Using web.xml
, you're forced to re-enter this class in web.xml
:
<servlet-class>com.foo.bar.Someservlet</servlet-class>
But wait, you've made a typo and you'll only discover it at runtime.
Or you rename a servlet class, but you forget to rename it in the web.xml as well, and you only discover the mistake at deployment time.
Finally, they make our life easier. You're creating a servlet, and you obviously want to map it to some URL. So you just add an annotation. No need to go to another file to add the mapping, then go back to the class because you forgot its exact name, then go back to the file again. Everything regarding a servlet is in the servlet class. Same for a filter, listener, etc.
Annotations don't have all these problems.
I hope this helps you!
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