Deployment Descriptor web.xml in Servlets

Deployment Descriptor web.xml in Servlets

1. What is Deployment?

Copying the .class file of the Servlet from the current directory to the classes folder of Tomcat (or any Web server) is known as deployment. When deployed, Tomcat is ready to load and execute the Servlet, at anytime, at the client request.

2. What is Deployment Descriptor?

As the name indicates, the deployment descriptor describes the deployment information (or Web Information) of a Servlet. The deployment descriptor is an XML file known as web.xml. XML is the easiest way to give the information to a server, just writing in between the tags, instead of writing in a text file or RDBMS file. The name and tags of web.xml are Servlet API specifications.

3. What information can be stored with deployment descriptor?

The following activities can be done by the programmer in web.xml file.
a) Mapping alias name with the actual Servlet name

First and foremost is the alias name to the Servlet. Never a client is given the actual name of the Servlet. Always an alias name is given just for security (avoid hacking). The alias name is given in the following XML tags.

image

The Servlet comes with two alias names, internal and external. The internal name is used by the Tomcat and the external name is given (to be written in <FORM> tag of HTML file) to the client to invoke the Servlet on the server. That is, there exists alias to alias. All this is for security. Observe, the names are given in two different XML tags, in the web.xml file, to make it difficult for hacking (for more security in EJB, two alias are given in two different XML files).

To invoke the Validation Servlet, the client calls the server with the name roses. When roses call reaches the server, the Tomcat server opens the web.xml file to check the deployment particulars. Searches such a <servlet-mapping> tag that matches roses. roses is exchanged with abcd. Then, searches such a <servlet> tag that matches abcd and exchanges with Validation. Now the server, loads Validation Servlet, executes and sends the output of execution as response to client.

b) To write Initialization Parameters

Intialization parameteres are read by the Servlet from web.xml file. Programmer can write code to be used for initialization. An example code is given below

This can be read by the Servlet using ServletConfig interface. It is more discussed in init param Example with ServletConfig.

c) To write tag libraries (this is mostly used in frameworks like Struts etc)

An example code is given used in Struts. This we will cover later.

Following are a few important XML elements in web.xml.

<context-param>, <filter-mapping>, <taglib> and <mime-mapping> etc.

Note 1: When a Servlet code is modified and copied to classes folder, each time it is required to restart the Tomcat server.

Note 2: For every Servlet, there must be an entry in web.xml file with an alias name.

4 thoughts on “Deployment Descriptor web.xml in Servlets

Leave a Reply

Your email address will not be published. Required fields are marked *