在 Java 构建路径上找不到超类“javax.servlet.http.HttpServlet”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22756153/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:43:38  来源:igfitidea点击:

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

javaeclipsejspmavenweb

提问by disable1992

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

我有一个由 Maven 集成在 Eclipse 中创建的项目。一切正常,但在所有 JSP 文件的工作空间中都有这样的:

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

To the first string where place:

到第一个字符串 where 的地方:

<%@ 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.

当我创建一个基本的动态 Web 项目和 JSP 时 - 一切都很好,没有错误。

回答by disable1992

Include servlet-api-3.1.jarin your dependencies.

包含servlet-api-3.1.jar在您的依赖项中。

  • Maven

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

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

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

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

回答by Anant Pathak

Select project → PropertiesProject FacetsTarget RuntimesVMware Server.

选择 project → PropertiesProject FacetsTarget RuntimesVMware Server

It worked for me.

它对我有用。

回答by DMF

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

项目 →属性目标运行时Apache Tomcat为我工作。Facets 下没有目标运行时(我使用的是Eclipse v4.2(Juno))。

回答by kas

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

由于这是未解决的问题,我猜测 ops 构建除了 Maven 依赖项之外还有其他问题。

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.

虽然不使用 Maven,但从 svn 重新创建我的开发环境时,我不时遇到同样的问题,我总是忘记原因,必须弄清楚。不幸的是,这似乎是 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.

我可以通过仅选择一个动态 Web 项目或仅依赖于 tomcat 的项目,并在构建顺序中移动依赖项,从一次工作项目中删除所有此类错误。这似乎迫使所有项目正确重建,然后解决所有错误。

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.

右键单击一个 Web 项目,选择“构建路径”->“配置构建路径”。转到“订购和导出”选项卡,然后选择一个库或 jar 条目并将其向上或向下移动。我使用了 JRE 系统库并将其移至顶部。

Click OK, and all that red goes away!

单击“确定”,所有红色都消失了!

回答by Mahder

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

如果您不使用 Maven,只需将 javax.servlet-api.jar 放在您的项目 lib 文件夹中。

回答by Ajil Mohan

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

首先添加运行时并选择项目属性。然后从“运行时”选项卡中检查服务器名称,如图所示。

Select runtime from Project Facets as shown the in image

从 Project Facets 中选择运行时,如图所示

回答by Burak Durmu?

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

项目 →属性目标运行时→ *Apache Tomcat 为我工作。Facets 下没有目标运行时(我使用的是Eclipse v4.4(Luna))。

回答by A. El Idrissi

Just add these dependencies to your pom.xmlfile:

只需将这些依赖项添加到您的pom.xml文件中:

<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>

回答by adarsh hegde

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

在服务器运行时添加 Tomcat 服务器将完成这项工作:

Project properties → Java Build PathAdd LibrarySelect "Server Runtime"from the list → Next→ Select "Apache Tomcat" → Finish.

项目属性→ Java 构建路径添加库→从列表中选择“服务器运行时”下一步→选择“Apache Tomcat”→完成

回答by shashank

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

对于 JBoss...右键单击项目 →构建 Java 路径→ 添加外部 JAR 文件。

Then browse to jboss-folder → Commonlib→ servlet-api.jar

然后浏览到 jboss-folder → Commonlib→ servlet-api.jar

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

. . 单击OK,刷新项目,然后运行它...