Search code examples
javaxmljsp

Getting ClassNotFound Error for Java Web Servlet


The application is running smoothly on Tomcat web server.

Whenever I try to access this java web servlet URL/prepare_payment i get the following error: SEVERE: Allocate exception for servlet [Servlet Name] java.lang.ClassNotFoundException: javax.servlet.http.HttpServlet

P.S: I am required to use JAVA 8

web.xml (Not Full):

<?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">
    <servlet>
        <servlet-name>PreparePaymentServlet</servlet-name>
        <servlet-class>com.paybylink.wnm_paybylink_app.servlets.PreparePaymentServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>PreparePaymentServlet</servlet-name>
        <url-pattern>/prepare_payment</url-pattern>
    </servlet-mapping>

    <error-page>
        <error-code>404</error-code>
        <location>/not-found.jsp</location>
    </error-page>

    <error-page>
        <error-code>500</error-code>
        <location>/error.jsp</location>
    </error-page>
</web-app>

pom.xml (Not Full):

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <junit.version>5.9.2</junit.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>jakarta.servlet</groupId>
            <artifactId>jakarta.servlet-api</artifactId>
            <version>4.0.4</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>jakarta.servlet.jsp</groupId>
            <artifactId>jakarta.servlet.jsp-api</artifactId>
            <version>3.1.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
</project>

And my servlet.java:

@SuppressWarnings("serial")
@WebServlet(name = "PreparePaymentServlet", urlPatterns = "/prepare_payment")
public class PreparePaymentServlet extends HttpServlet { 
...
}

Solution

    • You are mixing the wrong versions of the Servlet, JSP, etc. specifications with the wrong versions of Tomcat.
    • Furthermore, you are mixing the wrong package names with the wrong versions of those specifications.
    • And you are using the wrong version of Java.

    For the right versions of Servlet, JSP, etc. specifications, see the Which Version page of Tomcat web site.

    For Tomcat 10.1:

    • Servlet 6.0
    • JSP 3.1 (now known as Jakarta Server Pages rather than JSP)
    • EL 5.0 (now known as Jakarta Expression Language rather than EL)
    • WebSocket 2.1
    • Authentication Spec (JASPIC) 3.0

    … running on Java 11+.

    Fix these versions in your POM.

    These specs are part of Jakarta EE 10. Jakarta EE 10 uses the jakarta.* package naming rather than javax.*. This change in package names is part of the transition from Oracle donating these technologies to the Eclipse Foundation.

    Check these package names in the import statements in your code such as your Servlet class PreparePaymentServlet.

    You said:

    I am required to use JAVA 8

    Then you need to downgrade your version of Tomcat. And adjust your spec versions downwards accordingly. And revert to javax.* package names.

    Be aware that Java 8 has reached End-Of-Life. You may not be able to access updates and fixes without a paid support plan. I encourage you to move to a later LTS version of Java such as Java 17 or 21.