ChatGPT解决这个技术问题 Extra ChatGPT

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path [duplicate]

This question already has answers here: How do I import the javax.servlet / jakarta.servlet API in my Eclipse project? (16 answers) Closed 5 years ago.

I have a project created by Maven integration in Eclipse. All work fine, but in the work space in all JSP files have this:

The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

To the first string where place:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

When I create a basic dynamic web project and JSP in - all is fine, no errors.

You need to add the library in the build path. mkyong.com/maven/…
you might have missed the javax.servlet-api dependance in pom.xml
Be sure that you add it as provided dependency i.e. that it is not shipped with your web project.

P
Peter Mortensen

Add a runtime first and select project properties. Then check the server name from the 'Runtimes' tab as shown in the image.

https://i.stack.imgur.com/U46s9.png


Can you elaborate why this is working? How is the targeted runtime and a missing http-servlet connected? I'd be very interested in what's going on behind the curtains.
This error indicates that http-servlet is not available in the project class path, once we add target-runtime to the project , http-servlet will be available in the project class-path.
I did not have the Tomcat option in Runtimes in my Eclipse Mars, solved this in Window > Preferences > Server > Runtime Environments > Add > Apache Tomcat > chose de installation directory > Install
this is so out dated. in most case you use maven tomcat6 or 7 plugin.
In a project without using Maven, you can clear this error by right click your project name in Eclipse Project Explorer -> Properties -> Java Build Path, and check Apache Tomcat server to add it to your Java Build Path.
7
7 revs, 3 users 62%

Include servlet-api-3.1.jar in your dependencies.

Maven javax.servlet javax.servlet-api 3.1.0 provided

Gradle configurations { provided } sourceSets { main { compileClasspath += configurations.provided } } dependencies { provided 'javax.servlet:javax.servlet-api:3.1.0' }


a better answer would be Include servlet-api-x.y.jar in your dependencies. I though 3.0 was the latest.
error was gone, but on another pc i don't need do this.
How is this other PC different?
Only by Eclipse version, Kepler - error, Juno - ok.
The Servlet API Version to be used is determined by the container. So you need to know which is the traget system. To avoid classpath problems don't ship the servlet api with our artifact thus mark the depency als 'provided' in your pom.
P
Peter Mortensen

Project → PropertiesTarget RuntimesApache Tomcat worked for me. There is no Target Runtimes under Facets (I'm on Eclipse v4.2 (Juno)).


P
Peter Mortensen

For an Ant project:

Make sure, you have servlet-api.jar in the lib folder.

For a Maven project:

Make sure, you have the dependency added in POM.xml.

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>3.1.0</version>
  <scope>provided</scope>
</dependency>

Another way to do it is: Update the project facets to pick up the right server.

Check this box in this location:

Project → Properties → Target Runtimes → Apache Tomcat (any server)


The second suggestion worked for me. The project was created but the Tomcat server was not checked in "Target Runtimes".
P
Peter Mortensen

Project → PropertiesTarget Runtimes → *Apache Tomcat worked for me. There is no Target Runtimes under Facets (I'm on Eclipse v4.4 (Luna)).


P
Peter Mortensen

Adding the Tomcat server in the server runtime will do the job:

Project Properties → Target Runtimes → Select your Server from the list, "JBoss Runtime" → Finish

In case of Apache you can select Apache Runtime.

https://i.stack.imgur.com/9yBnq.png


C
Community

These steps can really help you:

If you didn't install any server you have to do these steps: Menu Window → Preferences → Expend Server → Runtime environment → Add → choose a name and then choose the Apache server path that you already installed on your PC (you can press download and install too) → Finish → OK

Ref# for more information, click here

Add the Tomcat server: Project Properties → Java Build Path → Add Library → Select "Server Runtime" from the list* → Next → Select "Apache Tomcat" → Finish

Ref# This answer


P
Peter Mortensen

Just add these dependencies to your pom.xml file:

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.0.1</version>
    <scope>provided</scope>
</dependency>

<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.2.1</version>
    <scope>provided</scope>
</dependency>

Why do we need the second dependency ? Please remove the second! (jsp-api).
no first one is enough.
P
Peter Mortensen

Adding the Tomcat server in the server runtime will do the job:

Project properties → Java Build Path → Add Library → Select "Server Runtime" from the list → Next → Select "Apache Tomcat" → Finish.


P
Peter Mortensen

Select project → Properties → Project Facets → Target Runtimes → VMware Server.

It worked for me.


k
kas

As this is unanswered, I am guessing something other than Maven dependencies are wrong with the ops build.

While not using Maven, I have the same problem from time to time when re-creating my development environment from svn, and I always forget why and have to figure it out. Unfortunately it seems this is a problem with Eclipse.

I am able to remove all such errors from once working projects by picking just one of the dynamic web projects, or just tomcat dependent projects, and move a dependency in the build order. This seems to force all projects to rebuild properly and all of the errors are then resolved.

Right click on a web project, select "build Path" -> "Configure Build Path". Go to the tab "Order and Export", then pick a library or jar entry and move it up or down. I used the JRE System Library and moved it to the top.

Click OK, and all that red goes away!


M
Mahder

If you are not using Maven, just drop the javax.servlet-api.jar in your project lib folder.


P
Peter Mortensen

In case of JBoss... right click on project → Build Java path → add external JAR files.

Then browse to jboss-folder → Common → lib → servlet-api.jar

. . Click OK, refresh the project, and run it...


Why is it only on Jboss ? Please remove that!
P
Peter Mortensen

And if nothing works by whatever reason, build it from the command line:

ant -Dj2ee.server.home=D:\apache-tomcat-8.0.23 clean

ant -Dj2ee.server.home=D:\apache-tomcat-8.0.23 compile

ant -Dj2ee.server.home=D:\apache-tomcat-8.0.23 dist