Java Servlet 3.0 Example using Web.xml

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:

Java Servlet 3.0 Example using Web.xml
Java Servlet 3.0 Example using Web.xml

Maven Dependency
We should add depedency for servlet such as below.

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

Configure Servlet 3 web.xml
You must register your servlet with servlet container using the tag and the . The tag include the full path of java class and servlet name that you want to map. The tag defines a specific URL that you want to use and map to the servlet via servlet name. Then we have the servlet configuration such as:

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.

Java Servlet 3.0 Example using Web.xml
Java Servlet 3.0 Example using Web.xml

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 (44 downloads)

Leave a Comment

*

Please share it if you found this useful
Hide Buttons