Easy learning with example program codes

Deployment Descriptor: web.xml file

Deployment Descriptor:

In a java web application a file named web.xml is known as deployment descriptor. It is a xml file and <web-app> is the root element for it. When a request comes web server uses web.xml file to map the URL of the request to the specific code that handle the request.

Sample code of web.xml file:


How web.xml works:

When a request comes it is matched with url pattern in servlet mapping attribute. In the above example all urls mapped with the servlet. You can specify a url pattern according to your need. When url matched with url pattern web server try to find the servlet name in servlet attributes same as in servlet mapping attribute. When match found control is goes to the associated servlet class.

Servlet “Hello World” example by extending HttpServlet class.

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 * This servlet program is used to print "Hello World" 
 * on client browser using HttpServlet class.
 * @author codesjava
public class HelloWorld extends HttpServlet {
    private static final long serialVersionUID = 1L;
    //no-argument constructor.
    public HelloWorld() {
    protected void doGet(HttpServletRequest request, HttpServletResponse 
             response) throws ServletException, IOException {
	PrintWriter out = response.getWriter();
        out.println("<h1>Hello World using HttpServlet class.</h1>");


<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" 


servlet example 3
Download this example.
Next Topic: welcome-file-list in web.xml with example.
Previous Topic: HttpServlet class in java with example.

Related Topics:

How to build java project using ant in eclipse?
JAXB marshalling – convert java object to xml example.
How to create pdf file in java using iText jar?
Generics class example.
OGNL in struts 2.
Hibernate One-to-One Mapping using xml.
Send inline image in email using JavaMail API.
Quartz 2 JobListener example.


Copyright © 2019 CodesJava Protection Status SiteMap Reference: Java Wiki