Home > Back-end >  Tomcat is looking for javax.servlet.Filter but I have jakarta.servlet.Filter
Tomcat is looking for javax.servlet.Filter but I have jakarta.servlet.Filter

Time:12-18

I created a dynamic web project on struts and when I tried to execute it, I get the error

java.lang.NoClassDefFoundError: javax/servlet/Filter Caused by: java.lang.ClassNotFoundException: javax.servlet.Filter

The problem is Tomcat is looking for javax.servlet.Filter but >I have jakarta.servlet.Filter

Tomcat Version : 10.0
Struts Version : 2.5

Could anyone please help? Thanks in advance.

Jakarta Library Image

Project Tree

enter image description here

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC 
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN" 
    "http://struts.apache.org/dtds/struts-2.5.dtd">  
<struts>
    <package name="first" extends="struts-default">
        <action name="Action" >  
            <result name="success">/success.jsp</result>   
            <result name="error">/error.jsp</result>  
        </action>
    </package>  
</struts>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="5.0">
  <display-name>FSGuild</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.jsp</welcome-file>
    <welcome-file>default.htm</welcome-file>
  </welcome-file-list>
  
  <filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>struts2</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

</web-app>

Action.java

package guild;

public class Action {
    public String execute() {
        System.out.println("Someone called execute");
        return "SUCCESS";
    }
}

error.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Error</title>
</head>
<body>
    <h1>ERROR</h1>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>SUCCESS</title>
</head>
<body>
    <h1>SUCCESS</h1>
</body>
</html>

CodePudding user response:

The problem is that you're using Struts, and Struts has not been updated to support Servlet specification 5.0 or higher (see https://issues.apache.org/jira/browse/WW-5141). Servlet specification 5.0 introduced the big breaking change of moving the API from the package javax.servlet to jakarta.servlet. This means that libraries, etc., still using the old package names cannot be used.

You need to switch to Tomcat 9 and define your application as a Servlet specification 4.0 web application (or Servlet specification 3.1).

CodePudding user response:

javax.servlet is the JavaEE specification (and Servlet spec <= 5.0), jakarta.servlet is the JakartaEE specification (and Servlet spec >= 6.0).

All you need to do is to switch from Tomcat 10.0 to 10.1.

See also

The only problem that could arise is if your application has classes for both specifications. Then you may want to find a container that can run on both specs in parallel, such as Wildfly 22.0.0 according to https://docs.wildfly.org/

  • Related