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

1

7

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.

Pargeting answered 20/3, 2022 at 4:39 Comment(0)
B
11

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>
Berkowitz answered 20/3, 2022 at 20:34 Comment(7)
This web.xml was in the ROOT web app in Tomcat 10. See tomcat.apache.org/tomcat-10.0-doc/index.htmlBerkowitz
Well, never mind. I just downloaded Tomcat 10.0.18. There I found basically the same text you posted. Except for one important difference: metadata-complete = "true" while you have false. I edited your Answer to include more info. Thanks for your Answer, and the suggest to look at the bundled web apps.Pargeting
@Basil thank you for professionalism and completing the information. I use annotations. So, I used the false value.Berkowitz
In Netbeans 13, when I do a "find usages", I get a warning message: "Potential changes cannot be propagated to web.xml because web application version is unsupported. Upgrade web.xml to version 2.4 or newer or use a previous version of NetBeans IDE." This is despite having updated the web.xml web-app element to version 5.0 as you have done. Using Tomcat10.0.21. Any ideas what it's complaining about?Watercool
@Berkowitz your example does not validate for me unless I provided a full URL to the XSD file: xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd"Gonium
Isn't that the same as the value given in the Tomcat example?Berkowitz
Likewise with 6.0. Doesn't validate: xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee web-app_6_0.xsd". Does: xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd".Teeters

© 2022 - 2024 — McMap. All rights reserved.