Skip to main content

Deployment Descriptor

Deployment Descriptor

  • Deployment descriptor acts like a instruction sheet to servlet container.
  • Web.xml is known as deployment descriptor for web application.
    It is an instruction sheet for web application.
  • If you have not configured web URL then Default it checks file in
    <welcome-file-list> attribute.
  • To configured Web URL We uses <servlet> and <servlet-mapping> two Attribute.
  • See web.xml file look like below
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">


 <display-name>FirstServlet</display-name>

 <welcome-file-list>
 <welcome-file>index.html</welcome-file>
 <welcome-file>index.htm</welcome-file>
 <welcome-file>index.jsp</welcome-file>
 <welcome-file>default.html</welcome-file>
 <welcome-file>default.htm</welcome-file>
 <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>


 <servlet>
 <servlet-name>FirstServlet</servlet-name>
 <servlet-class>com.spy.javaforlearn.FirstServlet</servlet-class>
 </servlet>


 <servlet-mapping>
 <servlet-name>FirstServlet</servlet-name>
 <url-pattern>/FirstServlet</url-pattern>
 </servlet-mapping>

</web-app>

<welcome-file-list>

  • It is used to configure default page path in web application.
  • if no resource URL present in web URL then container make use of this tag to locate the default resources in order.
<welcome-file-list>

<! static resources-->

<welcome-file>index.html</welcome-file>

<!dynamic resources-->

<welcome-file>currentdate</welcome-file>

</welcome-file-list>
  • Servlet must be access via Configured URL and web.xml defines mapping with URL and Servlet container uses this information to identified Servlet to handle given request.
 <servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>com.spy.javaforlearn.FirstServlet</servlet-class>
 </servlet>


<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
 <url-pattern>/FirstServlet</url-pattern>
</servlet-mapping>

Different ways to configure URL

Exact matching

<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>com.spy.javaforlearn.FirstServlet</servlet-class>
 </servlet>

<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
 <url-pattern>/FirstServlet</url-pattern>
</servlet-mapping>

Directory matching

<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>com.spy.javaforlearn.FirstServlet</servlet-class>
 </servlet>


<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
 <url-pattern>/xyz/*</url-pattern>
</servlet-mapping>

Pattern matching

<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>com.spy.javaforlearn.FirstServlet</servlet-class>
 </servlet>


<servlet-mapping>
<servlet-name>FirstServlet</servlet-name>
 <url-pattern>*.do</url-pattern>
</servlet-mapping>

Order of preferenmce

  1. Exact match
  2. Directory match
  3. Extension/pattern match

 

Using deployment descriptor we can customized container response in case of error scenario.Container will display an alternate page in such scenario.

<error-page>

<error-code>404</error-code>

<location>/404error.html</location>

</error-page>

 

<error-page>

<error-code>505</error-code>

<location>/505error.html</location>

</error-page>