You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JSP开发报错:无法实例化Servlet类com.example.demo.HelloServlet

Fixing ClassNotFoundException: jakarta.servlet.http.HttpServlet in JSP/Servlet Project

Hey there! Let's get to the bottom of this error—it's almost always tied to missing or mismatched Servlet API dependencies or a version conflict between your code and Tomcat. Here's how to fix it step by step:

1. Confirm Your Tomcat Version Matches Your Code

Your code uses jakarta.servlet.* packages (instead of the old javax.servlet.*), which means you need Tomcat 10.x or newer—Tomcat 10 was the first version to switch from Java EE to Jakarta EE. If you're using Tomcat 9 or lower, you’ll hit this exact error because those versions don’t include the jakarta.servlet classes.

  • Check your Tomcat version: Run bin/version.bat (Windows) or bin/version.sh (Linux/Mac) in your Tomcat directory.
  • If you’re on Tomcat 9 or older, either upgrade to Tomcat 10+, or rewrite your imports to use javax.servlet.* instead of jakarta.servlet.* (and update your web.xml schema to a Java EE version like 4/5/6/7/8 accordingly).

2. Add the Correct Servlet API Dependency (Maven/Gradle)

Even though Tomcat provides the Servlet API at runtime, your project needs the dependency to compile. The critical detail here is setting the scope to provided—this tells your build tool not to package the API into your WAR file (since Tomcat already includes it, avoiding conflicts).

For Maven projects:

Add this to your pom.xml:

<dependency>
    <groupId>jakarta.servlet</groupId>
    <artifactId>jakarta.servlet-api</artifactId>
    <version>5.0.0</version> <!-- Matches Tomcat 10 and your web.xml version 5.0 -->
    <scope>provided</scope>
</dependency>

For Gradle projects:

Add this to your build.gradle:

providedCompile 'jakarta.servlet:jakarta.servlet-api:5.0.0'

3. Verify IntelliJ Project Configuration

Make sure your IDE is set up to recognize the dependency and server correctly:

  • Go to File > Project Structure > Modules > Dependencies: Ensure the jakarta.servlet-api dependency is listed with the Provided scope.
  • Go to Run > Edit Configurations > Tomcat Server > Deployment: Confirm your project’s artifact (either war or exploded war) is added to the deployment list.
  • Check your project SDK: Tomcat 10 requires Java 8 or newer—go to File > Project Structure > Project Settings > Project and set the SDK to Java 8+.

4. Clean and Rebuild Your Project

Corrupted build files or cached data can sometimes cause unexpected issues. Do these steps:

  • Run a clean build: For Maven, execute mvn clean install in the terminal; for Gradle, run gradle clean build.
  • In IntelliJ, go to File > Invalidate Caches... > Invalidate and Restart to clear IDE caches and restart the application.

Note on Your Previous Fix Attempts

The changes you made to @WebServlet (adding urlPatterns, loadOnStartup) or using both annotations and web.xml aren’t the root cause here—those are valid configurations (though using either annotations OR web.xml is cleaner, not a requirement). The real issue is the missing Jakarta Servlet API dependency or version mismatch with Tomcat.

Once you fix the dependency and version alignment, your servlet should instantiate correctly!

内容的提问来源于stack exchange,提问作者Sahil Mambilly

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.27 13:57:37