<web-app> <description> <!-- 0 or more --> <display-name> <!-- 0 or more --> <icon> <!-- 0 or more --> <small-icon> <!-- 0 or 1 --> <large-icon> <!-- 0 or 1 --> </icon> <welcome-file-list> <welcome-file> <!-- 0 or more --> </welcome-file-list> <context-param> <!-- 0 or more --> <param-name> <!-- always 1 --> <param-value> <!-- always 1 --> </context-param> <distributable> <!-- optional --> <!-- Servlet, Filter, Security, and JSP configurations follow --> </web-app>Important: The element ordering within <web-app> is strictly enforced by the servlet specification. Elements must appear in the exact sequence defined by the schema, or the web container will reject the deployment descriptor during application startup.
<description>My Sample Web Application</description> <display-name>Sample Web Application</display-name> <icon> <small-icon>/images/mywebapp-icon-16.png</small-icon> <large-icon>/images/mywebapp-icon-32.png</large-icon> </icon>The <description> element provides detailed information about the application's purpose and functionality. The <display-name> offers a short, user-friendly name that administrative tools can display. Icon elements specify application icons for use in management interfaces.
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> </welcome-file-list>When a user accesses a directory URL (like http://mtitek.com/mywebapp/), the container will look for index.html first, then index.jsp, then default.html. The first file found will be served to the client. If no welcome files are found, the container's default behavior applies (typically showing a directory listing or a 404 error).
<context-param> <param-name>mywebapp.param1</param-name> <param-value>mywebapp-param1-value</param-value> </context-param> <context-param> <param-name>mywebapp.param1</param-name> <param-value>mywebapp-param1-value</param-value> </context-param>Context parameters can be retrieved programmatically using
ServletContext.getInitParameter("parameter-name")
.
This approach allows for externalized configuration without hardcoding values in the application source code.<servlet> <description>mywebapp requests</description> <display-name>mywebapp Servlet</display-name> <servlet-name>MyWebAppServlet</servlet-name> <servlet-class>com.mtitek.mywebapp.servlets.MyWebAppServlet</servlet-class> <init-param> <param-name>mywebapp.servlet.param1</param-name> <param-value>mywebapp-servlet-param1-value</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet>The <init-param> elements provide servlet-specific initialization parameters accessible through the ServletConfig interface. The <load-on-startup> element controls when the servlet is instantiated: negative values or absence means load on first request, zero or positive values indicate loading at startup with the numeric value determining loading order.
<servlet-mapping> <servlet-name>MyWebAppServlet</servlet-name> <url-pattern>/login</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>MyWebAppServlet</servlet-name> <url-pattern>/logout</url-pattern> </servlet-mapping>URL patterns can be exact matches (/login), path-based (/* or /admin/*), or extension-based (*.jsp). The servlet specification defines precedence rules for pattern matching when multiple patterns could apply to the same request.
<filter> <description>mywebapp requests filter</description> <display-name>mywebapp Filter</display-name> <filter-name>MyWebAppFilter</filter-name> <filter-class>com.mtitek.mywebapp.filters.MyWebAppServletFilter</filter-class> <init-param> <param-name>mywebapp.servlet.filter.param1</param-name> <param-value>mywebapp-servlet-filter-param1-value</param-value> </init-param> </filter>Filter initialization parameters work similarly to servlet parameters, providing configuration data accessible through the FilterConfig interface during filter initialization.
<filter-mapping> <filter-name>MyWebAppFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>FORWARD</dispatcher> </filter-mapping>The <dispatcher> element specifies when the filter should be applied: REQUEST (normal client requests), FORWARD (internal forwards), INCLUDE (includes), or ERROR (error page processing). Multiple dispatcher elements can be specified to apply the filter to different request types.
<session-config> <session-timeout>30</session-timeout> </session-config>The timeout value is specified in minutes. A value of 0 or negative means sessions never timeout automatically. This setting affects all sessions created within the web application.
<listener> <listener-class>com.mtitek.mywebapp.listeners.ApplicationStartupListener</listener-class> </listener>Listeners must implement appropriate listener interfaces such as ServletContextListener, HttpSessionListener, or ServletRequestListener depending on the events they need to handle.
<mime-mapping> <extension>pdf</extension> <mime-type>application/pdf</mime-type> </mime-mapping> <mime-mapping> <extension>json</extension> <mime-type>application/json</mime-type> </mime-mapping>These mappings affect the Content-Type header sent by the server when serving static files and can influence how browsers handle downloaded content.
<error-page> <error-code>404</error-code> <location>/error/404.html</location> </error-page> <error-page> <exception-type>java.lang.NullPointerException</exception-type> <location>/error/exception.jsp</location> </error-page>Error pages can be mapped to specific HTTP status codes or Java exception types. When an error occurs, the container forwards the request to the specified location, where error information is available through request attributes.
<security-role> <role-name>admin</role-name> </security-role> <security-role> <role-name>user</role-name> </security-role> <security-constraint> <web-resource-collection> <web-resource-name>Admin Area</web-resource-name> <url-pattern>/admin/*</url-pattern> <http-method>GET</http-method> <http-method>POST</http-method> </web-resource-collection> <auth-constraint> <role-name>admin</role-name> </auth-constraint> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint>The <web-resource-collection> defines the resources to protect, <auth-constraint> specifies which roles can access them, and <user-data-constraint> defines transport requirements (NONE, INTEGRAL, or CONFIDENTIAL for HTTPS).
<login-config> <auth-method>FORM</auth-method> <realm-name>Customer Portal</realm-name> <form-login-config> <form-login-page>/login.html</form-login-page> <form-error-page>/login-error.html</form-error-page> </form-login-config> </login-config>Authentication methods include BASIC (HTTP Basic), FORM (custom login form), DIGEST (HTTP Digest), and CLIENT-CERT (certificate-based). Form-based authentication requires specifying login and error page locations.