Explain Deployment descriptor
A deployment descriptor is an XML file used to configure a web application in Java EE (Enterprise Edition). This file is named web.xml and is located in the WEB-INF directory of a web application. It describes how the web application should be deployed on a server, including details about servlets, filters, listeners, security constraints, and other configurations.
Key Elements of a Deployment Descriptor
1. Â Servlets and Mappings
2. Â Filters and Mappings
3. Â Context Parameters
4. Â Session Configuration
5. Â Error Pages
6. Â Security Configurations

Table of Contents
1. Servlets and Mappings
Defines servlets and their URL patterns.
xml
<web-app>
<servlet>
<servlet-name>ExampleServlet</servlet-name>
<servlet-class>com.example.ExampleServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ExampleServlet</servlet-name>
<url-pattern>/example</url-pattern>
</servlet-mapping>
</web-app>
This configuration maps the ExampleServlet class to the URL pattern /example.
2. Filters and Mappings
Defines filters and their URL patterns or servlet names.
Example:
xml
<web-app>
<filter>
<filter-name>ExampleFilter</filter-name>
<filter-class>com.example.ExampleFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ExampleFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
This configuration applies ExampleFilter to all URL patterns (/*).
3. Context Parameters
Defines initialization parameters that are available to the entire web application.
Example:
xml
<web-app>
<context-param>
<param-name>configFile</param-name>
<param-value>/WEB-INF/config.properties</param-value>
</context-param>
</web-app>
This configuration makes the configFile parameter available to the application.
4. Session Configuration
Configures session timeout settings.
Example:
xml
<web-app>
<session-config>
<session-timeout>30</session-timeout> <!-- Timeout in minutes -->
</session-config>
</web-app>
This configuration sets the session timeout to 30 minutes.
5. Error Pages
Defines custom error pages for specific HTTP error codes or exceptions.
Example:
xml
<web-app>
<error-page>
<error-code>404</error-code>
<location>/error404.html</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error.jsp</location>
</error-page>
</web-app>
This configuration displays error404.html for HTTP 404 errors and error.jsp for any unhandled exceptions.
6. Security Configurations
Defines security constraints, login configurations, and security roles.
Example:
xml
<web-app>
<security-constraint>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<url-pattern>/protected/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>user</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.html</form-login-page>
<form-error-page>/error.html</form-error-page>
</form-login-config>
</login-config>
<security-role>
<role-name>user</role-name>
</security-role>
</web-app>
This configuration protects the /protected/* URL pattern and requires form-based authentication with a login page login.html and an error page error.html.
Summary
A deployment descriptor (web.xml) is a crucial part of Java EE web applications, providing a centralized configuration for:
- Servlets and Mappings : Define servlets and their URL patterns.
- Filters and Mappings : Apply filters to URL patterns or servlets.
- Context Parameters : Set initialization parameters for the application.
- Session Configuration : Configure session timeout settings.
- Error Pages : Define custom error pages.
- Security Configurations : Set up security constraints, login configurations, and roles.
The deployment descriptor allows for a declarative approach to configuring the web application, ensuring consistent and manageable application deployment.