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?
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:
servlet-api.jar
and select it.Or, if you copy the JAR into your project:
servlet-api.jar
in your project and select it.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.
_x000D_
Note: The jar which are shown in the screen are not correct jar.
you can follow the step to configure.
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).
Add to pom.xml
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>
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):
Edit: if there is no server listed you can create a new one on the Runtimes tab
Add the servlet-api.jar
to your classpath. You can take it from tomcat's lib folder.
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!).
You need to set the scope of the dependency to 'provided' in your POM.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.4</version>
<scope>provided</scope>
</dependency>
Then everything will be fine.
Source: Stackoverflow.com