Search code examples
javaservletsjakarta-eeweb.xml

Updating "web.xml" when transitioning from Java Servlet 4 to Jakarta Servlet 5


I am transitioning a simple Servlet from using Java Servlet 4 to Jakarta Servlet 5.

I noticed my web.xml file has references to the 4 spec.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns = "http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation = "http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version = "4.0">
</web-app>
  • What does that fragment do anyways?
  • How should I change those values to be appropriate the Jakarta Servlet 5?

I expect those javaee & 4 values should change.


Solution

  • Example from Tomcat

    Here is what I am using in my web.xml file.

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app
            xmlns = "https://jakarta.ee/xml/ns/jakartaee"
            xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
            xsi:schemaLocation = "https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"
            version = "5.0"
            metadata-complete = "false"
    >
        <display-name> Welcome to Tomcat</display-name>
        <description> Welcome to Tomcat</description>
    </web-app>
    

    This example is based on the web.xml file found within the ROOT web app bundled with Tomcat 10.0.x. Read section 8.1 Annotations and Pluggability of the Jakarta Servlet 5 spec to decide whether you want metadata-complete set to true or false.

    Example in Servlet spec

    See also an example of a deployment descriptor (web.xml) in section 14.4.1. A Basic Example of the Jakarta Servlet Specification, Version 5.0, Copyright (c) 2019, 2020 Eclipse Foundation.

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee
             web-app_5_0.xsd"
             version="5.0">
    
      <display-name>A Simple Application</display-name>
    
      <context-param>
        <param-name>Webmaster</param-name>
        <param-value>[email protected]</param-value>
      </context-param>
    
      <servlet>
        <servlet-name>catalog</servlet-name>
        <servlet-class>com.example.CatalogServlet</servlet-class>
        <init-param>
          <param-name>catalog</param-name>
          <param-value>Spring</param-value>
        </init-param>
      </servlet>
    
      <servlet-mapping>
        <servlet-name>catalog</servlet-name>
        <url-pattern>/catalog/*</url-pattern>
      </servlet-mapping>
    
      <session-config>
        <session-timeout>30</session-timeout>
      </session-config>
    
      <mime-mapping>
        <extension>pdf</extension>
        <mime-type>application/pdf</mime-type>
      </mime-mapping>
    
      <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.html</welcome-file>
      </welcome-file-list>
    
      <error-page>
        <error-code>404</error-code>
        <location>/404.html</location>
      </error-page>
    
    </web-app>