How to create a Servlet: Annotations (Servlet 3.0) vs. XML (up to Servlet 2.5)

In a previous article we saw how to create a new Servlet through the wizard provided by Eclipse IDE. One important thing to notice is that, after creating our Servlet and have included the Servlet API library, in the Deployment Descriptor of our project visible in the Project Explorer, new information have appeared under the item “Servlets” and “Servlet Mappings“.

create a simple web application - servlet imported

This is because, noting the class of our servlet with

@WebServlet(description = "Create new user Servlet", urlPatterns = { "/" })

we defined, configured and mapped the servlet into the application Deployment Descriptor.
At this point, however, we need to make an important clarification and then a step back to understand what it means what we just said.
The clarification is to say that annotations in the Servlet API have been only added in the version 3.0 of the specification, with the introduction of the javax.servlet.annotation package. Version 3.0 of the Servlet API specification has been introduced with version 6.0 of the Java Platform Enterprise Edition and the version of the Tomcat Servlet Container that provides its implementation (only of the Servlet API, not the whole EE platform) is the 7.0. Version 2.5 of the Servlet specification is instead part of the Java Platform Enterprise Edition 5.0, and is implemented from version 6.0 of Tomcat. The table shown in the following figure summarizes the correspondence between the versions of the Servlet API, versions of the Java EE platform and version of the Tomcat web container, always remembering that Tomcat, being just a web container (or servlet container) and not an Application Server, provides just the implementation of the Servlet and JSP APIs and not of the entire Java EE platform (of which Servlet and JSP APIs represent only a subset).

Servlet - JavaEE - Tomcat versions

The annotations introduced in the Servlet API 3.0 and included in the javax.servlet.annotation package are:
Java EE Servlet 3.0 annotations

Once made this important clarification, as mentioned, we step back to analyze the process of defining a servlet with the “traditional” method, prior to the introduction of annotations. This is based on XML configuration done via Deployment Descriptor, represented by an XML file called “ web.xml“.
The quickest way to do this is to take our Servlet class and comment the line containing the WebServlet annotation. We also press the keyboard shortcut “Ctrl + Shift + O” to remove its import, now unused.
In doing so we removed the definition and mapping of our Servlet from Deployment Descriptor, as we can see in the figure below where, in its navigation tree, no longer appears any information under “Servlets” and “Servlet Mappings” nodes.

Servlet 3.0 annotations

At this point we have created the servlet but, because we didn’t use the annotations, we have not yet declared and configured it.
To do this, as we have seen, we need the web.xml, the so-called Deployment Descriptor. To create it we right-click on “Deployment Descriptor: WebTest” of our project in the Project Explorer, and we select from the context menu the “Generate Deployment Descriptor stub” item. We will see appearing in our project, under the “WebContent/WEB-INF” section the web.xml file, where we will insert information necessary to the web container in order to deploy our servlet.

Eclipse - generate deployment descriptor stub

The sample of the Deployment Descriptor that is generated by Eclipse contains the following:

Autogenerated deployment descriptor

At this point we have to edit the Deployment Descriptor automatically generated by Eclipse, adding the definition of our servlet and its mapping with a URL that is the endpoint where the servlet will receive the requests. To do this we have to insert necessary information in the web.xml file, within tags and . The tag must contain the name of the Servlet and the class name used to implement it. The tag instead must contain, for each servlet defined in the previous tag, a URL to which the servlet is mapped to.

So, we edit the web.xml file adding:

	Add User


	Add User

The result of these modification is shown in the following picture:
Autogenerated deployment descriptor

Doing this we are at the same point where we were at the beginning of the article, when we used the WebServlet annotation to define and map the Servlet.

This entry was posted in $1$s. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *