The reason is the Java Servlet API is missing in the project's classpath. You can solve this problem by specifying a server runtime for the project, e.g. Apache Tomcat runtime – because a Java web server is a servlet container that implements the Servlet API.
It contains, among others, the files /usr/share/java/servlet-api-2.5. jar and /usr/share/java/jsp-api-2.1. jar , which are the servlet and JSP libraries you need.
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 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.
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.
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 you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With