Issue
I'm using jakarta.jakartaee-api 9.1.0 and H2 2.1.212.
I get this error when I try to add H2 Console Servlet to web.xml file:
'org.h2.server.web.WebServlet' is not assignable to 'javax.servlet.Servlet,jakarta.servlet.Servlet'
Here is my web.xml file:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="5.0"
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">
<data-source>
<name>java:global/dataSource</name>
<class-name>org.h2.jdbcx.JdbcDataSource</class-name>
<url>jdbc:h2:mem:test;MODE=LEGACY</url>
</data-source>
<servlet>
<servlet-name>H2Console</servlet-name>
<servlet-class>org.h2.server.web.WebServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>H2Console</servlet-name>
<url-pattern>/h2/*</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>jakarta.ws.rs.core.Application</servlet-name>
</servlet>
<servlet-mapping>
<servlet-name>jakarta.ws.rs.core.Application</servlet-name>
<url-pattern>/api/*</url-pattern>
</servlet-mapping>
</web-app>
How can I fix the error?
Solution
jakarta.*
packages
The package namespace for Jakarta 9 and 9.1 is jakarta.*
. This changed from the old javax.*
naming during the transition of Oracle handing over Java EE to the Eclipse Foundation where it became Jakarta EE.
I poked around GitHub to find JakartaWebServlet
. This seems to be the same Servlet but using the jakarta.*
packages. You may be able to switch from:
<servlet-class>org.h2.server.web.WebServlet</servlet-class>
… to:
<servlet-class>org.h2.server.web.JakartaWebServlet</servlet-class>
Answered By - Basil Bourque
Answer Checked By - Mary Flores (JavaFixing Volunteer)