Full Configuration Guide for web.xml in Java Web Applications

1. Core Structural Elements

  • <web-app>: Root element wrapping all configuration.
  • <display-name>: Human-readable name of the web application (used by tools and consoles).
  • <description>: Optional textual description of the application.
  • <icon>: Specifies small and large icons for GUI tooling.

2. Initialization and Context Parameters

<context-param> defines application-scoped parameters accessible via ServletContext.

<![CDATA[
<context-param>
    <param-name>appVersion</param-name>
    <param-value>2.1.0</param-value>
    <description>Application release version</description>
</context-param>
]]>

3. Filters and Filter Mappings

<filter> binds a filter class to a logical name; <filter-mapping> determines which requests it intercepts.

<![CDATA[
<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>com.example.filters.CharacterEncodingFilter</filter-class>
    <init-param>
        <param-name>charset</param-name>
        <param-value>UTF-8</param-value>
    </init-param>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
]]>

4. Listeners

<listener> registers event listeners for lifecycle events (e.g., session, context).

<![CDATA[
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
]]>

5. Servlets and URL Mappings

<servlet> declares a servlet class, including optional initialization parameters and load order.

<![CDATA[
<servlet>
    <servlet-name>ajaxServlet</servlet-name>
    <servlet-class>com.example.servlets.AjaxHandler</servlet-class>
    <init-param>
        <param-name>responseType</param-name>
        <param-value>json</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>ajaxServlet</servlet-name>
    <url-pattern>/api/*</url-pattern>
</servlet-mapping>
]]>

6. Session Configuration

<session-config> sets the default session timeout in minutes.

<![CDATA[
<session-config>
    <session-timeout>30</session-timeout>
</session-config>
]]>

7. MIME Type Mappings

<mime-mapping> maps file extensions to MIME types for correct response headers.

<![CDATA[
<mime-mapping>
    <extension>woff2</extension>
    <mime-type>font/woff2</mime-type>
</mime-mapping>
]]>

8. Welcome File List

<welcome-file-list> defines fallback files served when a directory is requested.

<![CDATA[
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>
]]>

9. Error Handling Pages

<error-page> allows mapping HTTP status codes or Java exceptions to custom pages.

<![CDATA[
<error-page>
    <error-code>404</error-code>
    <location>/errors/notfound.html</location>
</error-page>
<error-page>
    <exception-type>java.lang.OutOfMemoryError</exception-type>
    <location>/errors/oom.html</location>
</error-page>
]]>

10. Tag Library Declarations

Tag Library Descriptors (TLDs) can be aliased using <taglib>, optionally nested inside <jsp-config>.

<![CDATA[
<jsp-config>
    <taglib>
        <taglib-uri>/custom-tags</taglib-uri>
        <taglib-location>/WEB-INF/tlds/custom.tld</taglib-location>
    </taglib>
</jsp-config>
]]>

11. Resource References

External resources like databases or JMS queues are declared with <resource-ref> or <resource-env-ref>.

<![CDATA[
<resource-ref>
    <res-ref-name>jdbc/MyDS</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
</resource-ref>
]]>

12. Security Configuration

<security-constraint> defines protected URL patterns and allowed roles; <login-config> sets authentication method (e.g., FORM, BASIC).

<![CDATA[
<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>administrator</role-name>
    </auth-constraint>
</security-constraint>

<login-config>
    <auth-method>FORM</auth-method>
    <form-login-config>
        <form-login-page>/login.jsp</form-login-page>
        <form-error-page>/login-error.jsp</form-error-page>
    </form-login-config>
</login-config>
]]>

13. Security Roles

<security-role> declares a role used in security constraints.

<![CDATA[
<security-role>
    <role-name>administrator</role-name>
</security-role>
]]>

14. Environment Entries

<env-entry> defines JNDI environment entries for application configuraton.

<![CDATA[
<env-entry>
    <env-entry-name>maxUploadSize</env-entry-name>
    <env-entry-value>10485760</env-entry-value>
    <env-entry-type>java.lang.Integer</env-entry-type>
</env-entry>
]]>

15. EJB References

<ejb-ref> and <ejb-local-ref> declare remote or local EJB dependencies.

<![CDATA[
<ejb-local-ref>
    <ejb-ref-name>ejb/OrderProcessor</ejb-ref-name>
    <ejb-ref-type>Session</ejb-ref-type>
    <local-home>com.example.OrderHome</local-home>
    <local>com.example.Order</local>
</ejb-local-ref>
]]>

Tags: JavaEE Web.xml servlet Filter listener

Publicado em 9-11 00:01