JSP开发报错:无法实例化Servlet类com.example.demo.HelloServlet
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) orbin/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 ofjakarta.servlet.*(and update yourweb.xmlschema 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-apidependency is listed with theProvidedscope. - Go to Run > Edit Configurations > Tomcat Server > Deployment: Confirm your project’s artifact (either
warorexploded 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 installin the terminal; for Gradle, rungradle 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

