score:238

Accepted answer

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.

score: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).

score:7

if you get this compilation error, it means that you have not included the servlet jar in the classpath. the correct way to include this jar is to add the server runtime jar to your eclipse project. you should follow the steps below to address this issue: you can download the servlet-api.jar from here http://www.java2s.com/code/jar/s/downloadservletapijar.htm

save it in directory. right click on project -> go to properties->buildpath and follow the steps.

note: the jar which are shown in the screen are not correct jar.

you can follow the step to configure.

enter image description here

enter image description here enter image description here enter image description here

score:14

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

score:16

add to pom.xml

<dependency>
    <groupid>javax.servlet</groupid>
    <artifactid>javax.servlet-api</artifactid>
    <version>3.1.0</version>
</dependency>

score:18

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.

score: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

score:106

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!).


Related Query

More Query from same tag