438

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.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
disable1992
  • 4,673
  • 3
  • 15
  • 23
  • 1
    You need to add the library in the build path. http://www.mkyong.com/maven/maven-dependency-libraries-not-deploy-in-eclipse-ide/ – Jason Mar 31 '14 at 07:51
  • 8
    you might have missed the javax.servlet-api dependance in pom.xml – Karthigeyan Vellasamy Mar 31 '14 at 07:53
  • http://stackoverflow.com/a/16263523/1225526 – Karthigeyan Vellasamy Mar 31 '14 at 07:53
  • Be sure that you add it as `provided` dependency i.e. that it is not shipped with your web project. – andih Mar 31 '14 at 08:00
  • 1
    better answer at http://stackoverflow.com/q/4076601/1851302 – KNU Jun 19 '14 at 11:44
  • I'd like to know why things are shipped that don't work unless you google and read the answer on stack overflow! I know, job security for the volunteers, but wow I just installed STS, created a new maven project, and the error just showed up. Not just complaining here (well maybe a bit), but why do things end up like this a large portion of the time when concerning eclipse-built solutions? (not blaming eclipse specifically, just a correlation). – Dan Chase Feb 25 '17 at 23:11
  • I had a similar issue, which was based on the way I opened the project. Needed to import as maven project instead... details can be found here: https://stackoverflow.com/questions/45357109/maven-project-in-eclipse-org-springframework-cannot-be-resolved-to-a-type-from – James Oravec Jul 27 '17 at 18:23

14 Answers14

669

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Ajil Mohan
  • 7,099
  • 2
  • 14
  • 18
  • 9
    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. – atripes Oct 06 '14 at 08:54
  • 27
    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. – Ajil Mohan Oct 07 '14 at 17:19
  • 5
    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 – Tiago Oliveira de Freitas Oct 27 '15 at 22:07
  • 1
    this is so out dated. in most case you use maven tomcat6 or 7 plugin. – Junchen Liu Dec 25 '15 at 15:08
  • 1
    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. – user1457659 Jan 21 '16 at 21:03
  • 1
    If error still exists, select the project and press `ALT` + `F5` to update the project. – CDT Aug 12 '16 at 07:29
  • 1
    If someone is wondering where to find this window like I was.. It would appear when in 'Package Explorer' view > Right Click on your 'Project Folder' > select 'Properties'. On the Left panel, click on 'Project Facets'. There it is! – Murtuza K Nov 16 '16 at 10:05
  • It worked for me but I also had to do a project clean + rebuild (*Project -> Clean*) to get rid of the problem. – PJ_Finnegan Jun 27 '18 at 12:49
  • I have tried this solution and my problem still exists. I configured a runtime for was 8.5. – Pichitron Sep 10 '20 at 12:53
  • did someone found a solution?, i checked Tomcat in runtimes but the problem still there. – luis alberto juarez Oct 28 '20 at 04:55
252

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'
    }
    
Mr. Polywhirl
  • 31,606
  • 11
  • 65
  • 114
  • 9
    a better answer would be `Include servlet-api-x.y.jar in your dependencies.` I though 3.0 was the latest. – Scary Wombat Mar 31 '14 at 07:55
  • error was gone, but on another pc i don't need do this. – disable1992 Mar 31 '14 at 07:56
  • How is this other PC different? –  Mar 31 '14 at 07:57
  • 2
    Only by Eclipse version, Kepler - error, Juno - ok. – disable1992 Mar 31 '14 at 07:58
  • If you are using Maven to manage your dependencies and if the web container you deploy to (Tomcat?) doesn't differ, it should work. Do you deploy from inside Eclipse? –  Mar 31 '14 at 08:02
  • 5
    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. – andih Mar 31 '14 at 08:02
  • If you are using Maven, then it will solve the issue. – Pratik Aug 07 '14 at 15:43
  • I wonder why people downvote my answer. A comment would be useful. –  Sep 26 '14 at 06:32
  • where can I find this file? – user1079065 Oct 01 '14 at 01:52
  • This answer is almost completely right. The issue: `3.1` does not exist: http://mvnrepository.com/artifact/javax.servlet/javax.servlet-api shows what version *do* exist. I suggest you edit your answer to `3.0.1`or `3.1.0` (I would edit it but edits must be 6+ characters in length) – Don Cheadle Nov 10 '14 at 16:20
  • `3.1.0` it shall be then. –  Nov 10 '14 at 20:20
  • Once you specify a target runtime such as tomcat, you don't need to explicitly include the javax.servlet-api.jar. It will find it in the tomcat installation folder. – smwikipedia Dec 06 '15 at 14:53
116

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
DMF
  • 1,161
  • 1
  • 7
  • 4
59

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)

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Sireesh Yarlagadda
  • 10,572
  • 2
  • 65
  • 71
  • 1
    The second suggestion worked for me. The project was created but the Tomcat server was not checked in "Target Runtimes". – rabinnh Nov 27 '18 at 12:32
28

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Burak Durmuş
  • 867
  • 10
  • 14
24

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

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

In case of Apache you can select Apache Runtime.

Enter image description here

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Varun
  • 5,501
  • 18
  • 69
  • 107
21

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

Community
  • 1
  • 1
Chris Sim
  • 3,584
  • 3
  • 26
  • 32
15

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>
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
A. El Idrissi
  • 417
  • 7
  • 7
12

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

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
adarsh hegde
  • 1,233
  • 1
  • 20
  • 39
5

Select project → PropertiesProject FacetsTarget RuntimesVMware Server.

It worked for me.

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Anant Pathak
  • 147
  • 1
  • 4
  • 11
3

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!

kas
  • 93
  • 7
2

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

Mahder
  • 399
  • 3
  • 9
0

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

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

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

Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
0

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
Peter Mortensen
  • 28,342
  • 21
  • 95
  • 123
Puneet Pandey
  • 940
  • 2
  • 13
  • 26