164

I'm trying to use eclipse for Java EE to develop web applications.

I need to use Tomcat as my server. I've downloaded Tomcat and it's running. But my program doesn't compile.

I get the following error:

The import javax.servlet can't be resolved.

What do I need to do?

0

7 Answers 7

248

You need to add the Servlet API to your classpath. In Tomcat 6.0, this is in a JAR called servlet-api.jar in Tomcat's lib folder. You can either add a reference to that JAR to the project's classpath, or put a copy of the JAR in your Eclipse project and add it to the classpath from there.

If you want to leave the JAR in Tomcat's lib folder:

  • Right-click the project, click Properties.
  • Choose Java Build Path.
  • Click the Libraries tab
  • Click Add External JARs...
  • Browse to find servlet-api.jar and select it.
  • Click OK to update the build path.

Or, if you copy the JAR into your project:

  • Right-click the project, click Properties.
  • Choose Java Build Path.
  • Click Add JARs...
  • Find servlet-api.jar in your project and select it.
  • Click OK to update the build path.
8
  • 9
    Having a copy of a servletcontainer-specific JAR in /WEB-INF/lib would make your application unportable. It will only be able to run on the exact same server make/version and not on other server make/versions. NoClassDefFoundError would flying around your head.
    – BalusC
    Nov 7, 2010 at 22:24
  • I never said it should in in WEB-INF/lib - just that it could be copied into the project so that it can be added to the build path. That doesn't necessarily mean it gets included in the WAR. Nov 7, 2010 at 22:26
  • 5
    Still then, that's plain clumsy and not the recommended approach.
    – BalusC
    Nov 7, 2010 at 22:29
  • 8
    It is workaround solution. The exact solution is given by BalusC below. Dec 5, 2012 at 7:31
  • 2
    nothing changed adding servlet-api.jar, project > clean made it work.
    – kimchoky
    Sep 7, 2017 at 6:43
108

If not done yet, you need to integrate Tomcat in your Servers view. Rightclick there and choose New > Server. Select the appropriate Tomcat version from the list and complete the wizard.

When you create a new Dynamic Web Project, you should select the integrated server from the list as Targeted Runtime in the 1st wizard step.

Or when you have an existing Dynamic Web Project, you can set/change it in Targeted Runtimes entry in project's properties. Eclipse will then automagically add all its libraries to the build path (without having a copy of them in the project!).

1
  • 7
    Doesn't work for me, I have everything as you said, but still it can't resolve javax.servlet (using Tomcat 8.0.3 as server)
    – romanos
    Mar 9, 2014 at 8:10
20

You need to set the scope of the dependency to 'provided' in your POM.

http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope

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

Then everything will be fine.

18

I had the same problem because my "Dynamic Web Project" had no reference to the installed server i wanted to use and therefore had no reference to the Servlet API the server provides.

Following steps solved it without adding an extra Servlet-API to the Java Build Path (Eclipse version: Luna):

  • Right click on your "Dynamic Web Project"
  • Select Properties
  • Select Project Facets in the list on the left side of the "Properties" wizard
  • On the right side of the wizard you should see a tab named Runtimes. Select the Runtime tab and check the server you want to run the servlet.

Edit: if there is no server listed you can create a new one on the Runtimes tab

0
16

Add to pom.xml

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>
1
  • Just note that in-case this issue still happen when using maven then we only need to re-build the project again to let it recognize the change from the dependencies.
    – Cuong Vo
    May 23, 2019 at 2:09
14

Add the servlet-api.jar to your classpath. You can take it from tomcat's lib folder.

2

Had the same problem in Eclipse. For some reason I didn't have the servlet.jar file in my build path. What I wound up doing was copying a "lib" folder from another project of mine to the project I was working on, then manually going into that folder and adding the servlet.jar file to the build path (option shows up when you right-click on the file in the project explorer).

Not the answer you're looking for? Browse other questions tagged or ask your own question.