简体   繁体   English

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

[英]The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path

I'm a beginner and learning spring and hibernate (utilizing maven in project)and came across this problem and got stuck here.我是一名初学者,正在学习 spring 和 hibernate(在项目中使用 maven)并遇到了这个问题并被困在这里。 tried finding solution but ended up no where.试图找到解决方案,但最终无处可去。 Please help.请帮忙。 thanks a lot多谢

Error: "Can not find the tag library descriptor for " http://java.sun.com/jsp/jstl/core "错误:“找不到“ http://java.sun.com/jsp/jstl/core ”的标签库描述符

Code:代码:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>

Tried the solutions given in The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path How do I import the javax.servlet API in my Eclipse project?尝试了Java Build Path 中未找到超类“javax.servlet.http.HttpServlet”中给出的解决方案如何在我的 Eclipse 项目中导入 javax.servlet API?

The error still present错误仍然存​​在

pom.xml pom.xml

<?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 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.journaldev.spring</groupId>
<artifactId>SpringMVCHibernate</artifactId>
<name>SpringMVCHibernate</name>
<packaging>war</packaging>
<version>1.0.0-BUILD-SNAPSHOT</version>
<properties>
    <java-version>1.6</java-version>
    <org.springframework-version>4.0.3.RELEASE</org.springframework-version>
    <org.aspectj-version>1.7.4</org.aspectj-version>
    <org.slf4j-version>1.7.5</org.slf4j-version>
    <hibernate.version>4.3.5.Final</hibernate.version>
</properties>
<dependencies>
    <!-- Spring -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>${org.springframework-version}</version>
        <exclusions>
            <!-- Exclude Commons Logging in favor of SLF4j -->
            <exclusion>
                <groupId>commons-logging</groupId>
                <artifactId>commons-logging</artifactId>
             </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <!-- Hibernate -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-core</artifactId>
        <version>${hibernate.version}</version>
    </dependency>
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-entitymanager</artifactId>
        <version>${hibernate.version}</version>
    </dependency>

    <!-- Apache Commons DBCP -->
    <dependency>
        <groupId>commons-dbcp</groupId>
        <artifactId>commons-dbcp</artifactId>
        <version>1.4</version>
    </dependency>
    <!-- Spring ORM -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>${org.springframework-version}</version>
    </dependency>

    <!-- AspectJ -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>${org.aspectj-version}</version>
    </dependency> 

    <!-- Logging -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j-version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>jcl-over-slf4j</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>${org.slf4j-version}</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.15</version>
        <exclusions>
            <exclusion>
                <groupId>javax.mail</groupId>
                <artifactId>mail</artifactId>
            </exclusion>
            <exclusion>
                <groupId>javax.jms</groupId>
                <artifactId>jms</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jdmk</groupId>
                <artifactId>jmxtools</artifactId>
            </exclusion>
            <exclusion>
                <groupId>com.sun.jmx</groupId>
                <artifactId>jmxri</artifactId>
            </exclusion>
        </exclusions>
        <scope>runtime</scope>
    </dependency>

    <!-- @Inject -->
    <dependency>
        <groupId>javax.inject</groupId>
        <artifactId>javax.inject</artifactId>
        <version>1</version>
    </dependency>

    <!-- Servlet -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.5</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet.jsp</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>

    <!-- Test -->
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.7</version>
        <scope>test</scope>
    </dependency>

</dependencies>
<build>
    <plugins>
        <plugin>
            <artifactId>maven-eclipse-plugin</artifactId>
            <version>2.9</version>
            <configuration>
                <additionalProjectnatures>
                    <projectnature>org.springframework.ide.eclipse.core.springnature</projectnature>
                </additionalProjectnatures>
                <additionalBuildcommands>
                    <buildcommand>org.springframework.ide.eclipse.core.springbuilder</buildcommand>
                </additionalBuildcommands>
                <downloadSources>true</downloadSources>
                <downloadJavadocs>true</downloadJavadocs>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.5.1</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
                <compilerArgument>-Xlint:all</compilerArgument>
                <showWarnings>true</showWarnings>
                <showDeprecation>true</showDeprecation>
            </configuration>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.2.1</version>
            <configuration>
                <mainClass>org.test.int1.Main</mainClass>
            </configuration>
        </plugin>
    </plugins>
    <finalName>${project.artifactId}</finalName>
</build>

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

Project Properties-> Java Build Path-> Add Library -> Select "Server Runtime" from the list-> Next->Select "Apache Tomcat"-> Finish

Adding below dependency in pom.xml, worked for me.在 pom.xml 中添加以下依赖项,对我有用。

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
</dependency>

I too received the same error as quoted below:我也收到了与下面引用的相同的错误:

The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path.在 Java 构建路径中找不到超类“javax.servlet.http.HttpServlet”。

I followed these steps to resolve the issue:我按照以下步骤解决了这个问题:

  1. Right Click on Project右键单击项目
  2. Select Properties选择属性
  3. Select Project Facets选择项目构面
  4. Select Apache Tomcat as Runtime server选择 Apache Tomcat 作为运行时服务器
  5. Click OK单击确定

for maven project , you should add this dependency对于 maven 项目,您应该添加此依赖项

<dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
</dependency>

I came across the same issue.我遇到了同样的问题。 I tried adding the server in "Server Runtime" but unfortunately that didn't work for me.我尝试在“服务器运行时”中添加服务器,但不幸的是这对我不起作用。

What worked for me is, I added javax.servlet-api-3.0.1.jar file in build path.对我javax.servlet-api-3.0.1.jar是,我在构建路径中添加了javax.servlet-api-3.0.1.jar文件。 On the other hand If It's a Maven project add dependency for this jar file.另一方面,如果它是一个 Maven 项目,请为此 jar 文件添加依赖项。 This would definitely work.这肯定会奏效。

add servlet-api jar file which is present in the lib folder under tomcat folder.添加 servlet-api jar 文件,该文件位于 tomcat 文件夹下的 lib 文件夹中。 You can do this as follows Project -> properties -> java build path -> under libraries click add external jars after that go to project -> clean -> build all.您可以按以下方式执行此操作 Project -> properties -> java build path -> 在库下单击 add external jars,然后转到 project -> clean -> build all。 this worked for me.这对我有用。

Add servlet-api.jar file which is present in the lib folder under tomcat folder.添加 servlet-api.jar 文件,该文件位于 tomcat 文件夹下的 lib 文件夹中。 You can do this using the following steps:您可以使用以下步骤执行此操作:

1.Select project properties 1.选择项目属性
2.Select Java Build Path 2.选择Java构建路径
3.Select Libraries 3.选择库
4.Select External Jars 4.选择外部罐子
5.Select servlet-api.jar 5.选择servlet-api.jar
6. Apply & Ok. 6. 申请并确定。

The issue should be resolved after these steps.执行这些步骤后,问题应该得到解决。

Adding the Tomcat server in the server runtime will do the job:在服务器运行时添加 Tomcat 服务器将完成这项工作:

Right click your project and than;右键单击您的项目,然后;

Project properties → Target Runtimes → Select/Check "Apache Tomcat" → Finish.项目属性→目标运行时→选择/检查“Apache Tomcat”→完成。

I had this same issue and a couple others being reported for a existing maven project.我遇到了同样的问题,并且针对现有的 Maven 项目报告了其他几个问题。

I had the proper dependencies in place and I could see the jar under maven dependencies, however the project was improperly brought into eclipse.我有适当的依赖项,我可以在 maven 依赖项下看到 jar,但是该项目被错误地引入了 eclipse。

I ended up having to delete the project, clone from git again then do an import of the project as an existing maven project.我最终不得不删除该项目,再次从 git 克隆,然后将该项目作为现有的 maven 项目导入。

This solved the issue in this thread and several others issues I was having.这解决了该线程中的问题以及我遇到的其他几个问题。 More details on solution can be found here: Maven Project in Eclipse `org.springframework cannot be resolved to a type` from target path可以在此处找到有关解决方案的更多详细信息: Eclipse `org.springframework 中的 Maven 项目无法解析为目标路径中的类型`

始终检查您的java 文件是否在src/main/java 中,而不是在其他目录路径中。

试试这个 - 在运行时添加 Tomcat

For an Ant project:对于 Ant 项目:

Make sure, you have servlet-api.jar in the lib folder.确保在 lib 文件夹中有 servlet-api.jar。

For a Maven project:对于 Maven 项目:

Make sure, you have the dependency added in POM.xml.确保在 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)项目 → 属性 → 目标运行时 → Apache Tomcat(任何服务器)

Your system can have many different tomcat versions.您的系统可以有许多不同的 tomcat 版本。 You can try to solve it.你可以尝试解决。

Right Click on Project then Select Properties, Select Project Facets and on the right section, Select right Apache Tomcat versions as Runtimes and click ok右键单击 Project,然后选择 Properties,选择 Project Facets,在右侧部分,选择正确的 Apache Tomcat 版本作为 Runtimes,然后单击 OK

I faced the same error.我遇到了同样的错误。 Adding the tomcat server in the runtime environment will solve the error:在运行环境中添加tomcat服务器会解决错误:

Right click on your project -> Properties -> Targeted runtimes -> Select apache tomcat server -> click apply -> click ok.右键单击您的项目 -> 属性 -> 目标运行时 -> 选择 apache tomcat 服务器 -> 单击应用 -> 单击确定。

The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path在 Java 构建路径上找不到超类“javax.servlet.http.HttpServlet”

Error: "Can not find the tag library descriptor for " http://java.sun.com/jsp/jstl/core "错误:“找不到“ http://java.sun.com/jsp/jstl/core ”的标签库描述符

Solution: 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解决方案:在服务器运行时添加 tomcat 服务器将完成这项工作:项目属性-> Java 构建路径-> 添加库-> 从列表中选择“服务器运行时”-> 下一步-> 选择“Apache Tomcat”-> 完成

This solution work for me.这个解决方案对我有用。

you are getting this error because of the server is not enabled by default ie you don't have any runtime chosen for that is why you are getting the error so, for that you need to do the following steps to choose the runtime.您收到此错误是因为默认情况下未启用服务器,即您没有为此选择任何运行时,这就是您收到错误的原因,为此您需要执行以下步骤来选择运行时。

Follow The Path right-click on the project --> GoTo Properties--> Click on Targeted Runtimes-->then click on the checkbox ie Apache tomcat or other servers which you are using --->then click on apply and then apply and close按照路径右键单击项目--> 转到属性--> 单击目标运行时--> 然后单击复选框,即 Apache tomcat 或您正在使用的其他服务器 ---> 然后单击应用,然后应用并关闭

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM