Instead of using annotation such as the previous post. In this tutorial we are building a java servlet 3.0 example using web.xml. The servlet is configured in web.xml. It means that you tell the servlet container or application server about the detail of servlet. This is second post of series of Java Servlet Tutorial. This series tutorial will provide you full knowledge about Servlet 3.0.
Project structure
The project structure should be such as the figure below:

Maven Dependency
We should add depedency for servlet such as below.
1 2 3 4 5 6 7 8 |
<dependencies> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> <!-- Maven does not include the library in the final built artifact --> </dependency> </dependencies> |
Servlet Container or Application Server has built in servlet support so we need to set the scope is provided.
Define your servlet
Your servlet must extends the javax.servlet.http.HttpServlet abstraction. The HttpServlet abstract subclass also adds additional methods of the basic Servlet interface. These methods are such as:
doGet: handling HTTP GET requests
doPost: handling HTTP POST requests
doPut: handling HTTP PUT requests
doDelete: handling HTTP DELETE requests
doHead: handling HTTP HEAD requests
doOptions: handling HTTP OPTIONS requests
doTrace: handling HTTP TRACE requests
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package com.javabycode; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class HttpServletDemo extends HttpServlet{ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter printOut = response.getWriter(); printOut.write("Hello, This servlet is configured in web.xml"); } } |
Configure Servlet 3 web.xml
You must register your servlet with servlet container using the
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<web-app 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" version="3.1"> <servlet> <servlet-name>Demo-Servlet</servlet-name> <servlet-class>com.javabycode.HttpServletDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>Demo-Servlet</servlet-name> <url-pattern>/servlet/*</url-pattern> </servlet-mapping> </web-app> |
Deploy war file on application server.
After push the file war and run application server we access the address http://localhost:8080/servlet-demo/servlet/Demo-Servlet. The result should be such as the figure below.

That’s all. You can also find other servlet example in the series of Java Servlet Example.
Download complete source code, please click link below
Servlet-3-Web-xml-Configuration-Example.zip (315 downloads)