163

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?

Ahmad
  • 12,336
  • 6
  • 48
  • 88
snakile
  • 52,936
  • 62
  • 169
  • 241

7 Answers7

247

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.
Borjovsky
  • 758
  • 2
  • 10
  • 24
Richard Fearn
  • 25,073
  • 7
  • 56
  • 55
  • 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 07 '10 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. – Richard Fearn Nov 07 '10 at 22:26
  • 5
    Still then, that's plain clumsy and not the recommended approach. – BalusC Nov 07 '10 at 22:29
  • 8
    It is workaround solution. The exact solution is given by BalusC below. – Ahmet Karakaya Dec 05 '12 at 07:31
  • 2
    nothing changed adding servlet-api.jar, **project** > **clean** made it work. – kimchoky Sep 07 '17 at 06:43
  • Moved Apache tomcat directory after creating a New project, just creating new project in new project workspace with new tomcat server directory worked easily. – Manohar Reddy Poreddy Aug 23 '18 at 04:07
  • Running a Struts 1.x project on WebLogic 9 cannot resolve `import javax.servlet.http.HttpServletRequest` in `ActionForm`. After added `servlet-api.jar` into build path, the error goes away. Thanks. – not_Prince Nov 09 '18 at 07:05
  • Adding the external servlet api worked for me – kobewarui Mar 07 '23 at 12:03
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!).

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 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 09 '14 at 08:10
19

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.

Jeroen Rondeel
  • 259
  • 3
  • 4
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

flumingo
  • 513
  • 2
  • 7
  • 24
16

Add to pom.xml

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
</dependency>
Manwal
  • 23,450
  • 12
  • 63
  • 93
Andrey
  • 1,528
  • 14
  • 12
  • 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 '19 at 02:09
14

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
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).

YMW
  • 66
  • 5