Using the deployment descriptor: web.xml

With the wide use of the java platform enterprise edition in most web applications now, making full use of the deployment descriptor DD is essential. The web.xml provides a suitable mechanism for making your web applications customizable without the need to mess with the code. This is done by determining how URLs map to servlets requiring authentication and other information. The web.xml resides in the app’s WAR under the WEB-INF/ directory and is a part of the servlet standard for web applications.

How do deployment descriptors work with regard to your web application? It describes classes, resources, and configuration of your application as to how the web server users these components to serve web requests. The web server in turn, upon receipt of the request, uses the deployment descriptor to map the URL of the request to the proper code that handles the request.

This is a simple web.xml file content shown here to the servlet class coo.master.CollectMeServlet:

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<servlet><servlet-name>collectme</servlet-name>
<servlet-class>coo.master.CollectMeServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>collectme</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

How to map a URL to a servlet?

When you use the mappings between URL paths from the web.xml, the web server finds the configuration identifying the servlet that handles a given request and calls the class method referring to the request method. Mapping is done by declaring the servlet with the <servlet> element then defining a mapping from the URL path to a servlet declaration using the <servlet-mapping> element.

The element <servlet> serves as declaration of the name of the servlet to be used by other calling elements. You can declare multiple servlets using same class but with different initialization parameters. The naming for each should be unique across the deployment descriptor.

<servlet><servlet-name>fruit</servlet-name>
<servlet-class>coo.fridge.DishServlet</servlet-class>
<init-param>
<param-name>dishcolor</param-name>
<param-value>green</param-value>
</init-param>
<init-param>
<param-name>truecolor</param-name>
<param-value>#00CC00</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>veggie</servlet-name>
<servlet-class>coo.fridge.DishServlet</servlet-class>
<init-param>
<param-name>truecolor</param-name>
<param-value>#CC0000</param-value>
</init-param>
<init-param>
<param-name>dishcolor</param-name>
<param-value>red</param-value>
</init-param>
</servlet>

The element <servlet-name> is used to specify a URL pattern and declared the named servlet to use for requests matching the URL pattern. An asterisk (*) can be used in the URL pattern at the start or end of the pattern to denote zero or more characters of any kind. Note that in the standard, wildcards are not supported in the middle of a string without allowing multiple wildcards in one pattern. The matching of the pattern is done to the full path of the URL, starting with and including the forward slash after the domain name.

Below is an example to illustrate a request for the URL http://www.cocomo.com/red/veggiecolor is handled by the DishServlet class, with dishcolor parameter equal to green and truecolor equal to #00CC00. The servlet gets the portion of the URL path matched by the wildcard using the ServletRequest object’s getPathInfo() method.

<servlet-mapping>
<servlet-name>greenveggie</servlet-name>
<url-pattern>/green/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>redveggie</servlet-name>
<url-pattern>/red/*</url-pattern>
</servlet-mapping>

The servlet may the access the pertinent initialization parameters by getting its servlet configuration using its own getServletConfig() method, then calling the getInitParameter() method on the configuration object using the name of the parameter as an argument.

String dishcolor = getServletConfig().getInitParameter("dishcolor");

Image by Stuart Miles (freedigitalphotos.net)

Article By: Sanjay Modasia

Sanjay is our lead developer with over 6 years experience, handling a strong team of developers with various industry expertise working on projects and meeting deadlines.


Comments are closed.

Three Measures To Keep Track Of A Cell Mobile Phone appforspy.com mobile tracker app