Deployment Descriptor
In a JEE (Java Enterprise Edition) application, you may need a web.xml file under the WEB-INF folder, which details many of the components and configurations that make up the web application. This web.xml file is a JEE standard and is also called the Deployment Descriptor. If provided, it must be under the WEB-INF folder.
Here is an example of one such file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app
version="3.1"
metadata-complete="false"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
Servlet Mappings
If you set the <web-app> element's version attribute to a number below 3.0, then every servlet must have its definition and mapping entries in the web.xml file. Here is an example:
<servlet>
<servlet-name>Simple</servlet-name>
<servlet-class>com.mbcc.SimpleServlet</servlet-class>
<description>This is a simple Hello World servlet</description>
</servlet>
<servlet-mapping>
<servlet-name>Simple</servlet-name>
<url-pattern>/servlet/SimpleServlet</url-pattern>
</servlet-mapping>
Every servlet must have two elements defined in the web.xml file: a <servlet> element, which defines the <servlet-name> of the servlet, and the <servlet-class>, which defines the full class path of the servlet.
The second required element is <servlet-mapping>, which defines the <servlet-name> that matches the name given in the <servlet> element and the <url-pattern>, which defines the path that should be used to invoke this servlet.
web-app version 3.0 and beyond
With version 3.0 and beyond of the web-app, you do not need to provide the servlet definitions in the web.xml file. Instead, you provide the @WebServlet annotation in the servlet's Java class. Once this annotation is provided, the two entries in the web.xml file are not required.
Here is an example of how the exact same servlet can be represented with an annotation:
package com.mbcc;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class TestServlet
*/
@WebServlet("/servlet/SimpleServlet")
public class SimpleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
Ensure that you remove the two elements that were added for the servlet in web.xml after the annotation is added. Otherwise, you will see an exception, as only one way of mapping is permitted: either via an annotation or via entries in web.xml.
References: