Java Web Container and Deployment Descriptor (web.xml) -

Java Web Container or Controller -

Web Container or Servlet Container is a part of Web Server that interacts with servlets. When a request comes in, it first goes to Container, it analyze the request and maps it to correct servlets.
The main responsibilities of Web Container are as follows -
  • Servlets do not have main method, they are instantiated by Container.
  • Creates threads to handle the request.
  • Call doGet() or doPost() method based on HTTP method used.
  • Create HTTP Request and Response objects.
  • Load and unload servlet.

Some open source web containers are as follows -

1- Apache Tomcat from Apache Software Foundation.
2- GlassFish from Oracle.

Container provides the following advantages -

Life Cycle Management - Manage life and death of servlets.
Multithreading support - Automatically creates threads for every servlet request it receives.
Security - It defines a XML file named Deployment Descriptor that allows you to easily add or modify security credential as required.

When a request comes in to Container, the step by step responsibilities of Container are as follows -

1- User clicks a link that has a request to new page.

2- Container creates HttpServletRequest and HttpServletResponse objects.

3- Container finds the correct servlet based on URL in request, and create and assign a thread to it and pass request and response objects to that thread.

4- Next it calls the service method of servlet that generate dynamic page and put the page into response object and response sends back to Container.

5- Finally the task of thread has completed. Container convert the response into HTTP Response send back to client. Finally delete request and response objects.

Deployment Descriptor (web.xml) -

  • When we deploy our application on server, we need to create a XML file called web.xml, to tell the web container, how to run our Servlet,s. This web.xml file is also called Deployment Descriptor. (because it contains information regarding deployment of your application.)
  • In this file we have to put all the information regarding our servlets like servlet name, servlet class, url etc. Basically the main purpose of this is to create a mapping between client known public url(shown in client Browser URL box) and servlet class name.
  • In addition to servlet information , web.xml file also used to contain several other information that will be discussed ahead.
For mapping client known public url (shown in client Browser URL box)and servlet class name, we need to define following two XML tags -
TO better understand this, letus consider we have a link defined as follows-
<a href="MyServlet">Click Here<a>
On the click of this link we want to call a Servlet defined by class 'ServletDemo'. i.e. package servs import ..... class ServletDemo extends HtttpServlet { ......... ......... } So in this case - 'MyServlet' - will be client known public url. 'ServletDemo' - will be servlet class name , that we have to map to public url.

1- <servlet> -

It maps internal name defined in web.xml to your original servlet class name.
It contains two sub tags within it -
1.1- <servlet-name> - It contain internal name defined in web.xml (We used 'demo' in our example). This name can be anything, whatever you want. It is required to map client public url and servlet class name.
1.2- <servlet-class> - It contain fully qualified servlet class name with package name ('servs.ServletDemo' in our example).

2- <servlet-mapping> -

It maps internal name defined in web.xml to client known public url.
It also contains two sub tags within it -
2.1- <servlet-name> - It will be the same as we defined in <servlet> tag(i.e. demo in our example). 2.2- <url-pattern> - It will be the client known public url ('/MyServlet' in our example, because we use 'MyServlet' as hyperlink in anchor tag).
We need to add the above defined both tags in web.xml within web-app as follows -
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns=""

Whenever a client request come to Web container, It makes use of Deployment Descriptor to call correct servlet based on client url as follows -

1- User clicks a link that has a request to new page (MyServlet).

2- Container goes to web.xml and find <servlet-name> corresponding to client url (MyServlet) in <servlet-mapping> tag, and finally find the correct <servlet-class> that has to be executed from <servlet> tag

3- FInally container send generated response to client.