The superclass “javax.servlet.http.HttpServlet” wa

2020-01-27 08:51发布

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.

14条回答
家丑人穷心不美
2楼-- · 2020-01-27 09:25

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!

查看更多
Lonely孤独者°
3楼-- · 2020-01-27 09:26

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

查看更多
做个烂人
4楼-- · 2020-01-27 09:27

These steps can really help you:

  1. If you didn't install any server you have to do these steps:

    Menu WindowPreferencesExpend ServerRuntime environmentAdd → choose a name and then choose the Apache server path that you already installed on your PC (you can press download and install too) → FinishOK

Ref# for more information, click here

  1. Add the Tomcat server:

    Project PropertiesJava Build PathAdd Library → Select "Server Runtime" from the list* → Next → Select "Apache Tomcat" → Finish

Ref# This answer

查看更多
走好不送
5楼-- · 2020-01-27 09:29

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>
查看更多
何必那么认真
6楼-- · 2020-01-27 09:30

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

  • 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'
    }
    
查看更多
放我归山
7楼-- · 2020-01-27 09:30

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 → PropertiesTarget Runtimes → Apache Tomcat (any server)

查看更多
登录 后发表回答