Vitaly Menchikovsky
Vitaly Menchikovsky

Reputation: 8934

Can't import javax.servlet.annotation.WebServlet

I have started to write app that can run on Google App Engine.
But when I wanted to use my code from Netbeans to Eclipse I had an errors on:

import javax.servlet.annotation.WebServlet;

and

@WebServlet(name = "MyServlet", urlPatterns = {"/MyServlet"})

the errors are:

The import javax.servlet.annotation cannot be resolved
WebServlet cannot be resolved to a type

I tried to import the servlet-api.jar to Eclipse but still the same, also tried to build and clean the project. I don't use Tomcat on my Eclipse only have it on my Netbeans. How can I solve the problem?

Upvotes: 37

Views: 134315

Answers (12)

Zin Myo Swe
Zin Myo Swe

Reputation: 938

Copy servlet-api.jar from your tomcat server lib folder.

enter image description here

Paste it to WEB-INF > lib folder

enter image description here

Error was solved!!!

Upvotes: 2

toludav
toludav

Reputation: 31

Simply add the below to your maven project pom.xml flie:

<dependencies>
          <dependency>
            <groupId>javax</groupId>
            <artifactId>javaee-web-api</artifactId>
            <version>6.0</version>
            <scope>provided</scope>
         </dependency>
  </dependencies>

Upvotes: 3

Piyush Chordia
Piyush Chordia

Reputation: 1375

If you are using IBM RAD, then ensure the to remove any j2ee.jar in your projects build path -> libraries tab, and then click on "add external jar" and select the j2ee.jar that is shipped with RAD.

Upvotes: 1

jansohn
jansohn

Reputation: 2346

If you're using Maven and don't want to link Tomcat in the Targeted Runtimes in Eclipse, you can simply add the dependency with scope provided in your pom.xml:

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

Upvotes: 13

Khaino
Khaino

Reputation: 4169

Go to

window->Preference->server->runtime environment

then choose your tomcat server. If the error is still there, then

right click project->properties>Targeted Runtimes

then check the server

Upvotes: 5

murillo.cjr
murillo.cjr

Reputation: 41

I had the same issue using Spring, solved by adding Tomcat Server Runtime to build path.

Upvotes: 0

Sobbosachi
Sobbosachi

Reputation: 21

import javax.servlet.annotation.*;

(no one has written this, but need to import this as WebInitparam is not recognized by the other packages)

Upvotes: 2

darkstar_mx
darkstar_mx

Reputation: 486

I had the same issue, it turns that I had my project configured as many of you stated above, yet the problem persisted, so I went deeper and came across that I was using Apache Tomcat 6 as my Runtime Server, so the fix was simple, I just upgraded the project to use Apache Tomcat 7 and that's all, finally the IDE recognized javax.servlet.annotation.WebServlet annotation.

Upvotes: 2

user2038106
user2038106

Reputation: 11

Add library 'Server Runtime' to your java build path, and it shall resolve the issue.

Upvotes: 1

BalusC
BalusC

Reputation: 1109695

I tried to import the servlet-api.jar to eclipse but still the same also tried to build and clean the project. I don't use tomcat on my eclipse only have it on my net-beans. How can I solve the problem.

Do not put the servlet-api.jar in your project. This is only asking for trouble. You need to check in the Project Facets section of your project's properties if the Dynamic Web Module facet is set to version 3.0. You also need to ensure that your /WEB-INF/web.xml (if any) is been declared conform Servlet 3.0 spec. I.e. the <web-app> root declaration must match the following:

<web-app
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

In order to be able to import javax.servlet stuff, you need to integrate a fullworthy servletcontainer like Tomcat in Eclipse and then reference it in Targeted Runtimes of the project's properties. You can do the same for Google App Engine.

Once again, do not copy container-specific libraries into webapp project as others suggest. It would make your webapp unexecutabele on production containers of a different make/version. You'll get classpath-related errors/exceptions in all colors.

See also:


Unrelated to the concrete question: GAE does not support Servlet 3.0. Its underlying Jetty 7.x container supports max Servlet 2.5 only.

Upvotes: 59

palacsint
palacsint

Reputation: 28895

Check that the version number of your servlet-api.jar is at least 3.0. There is a version number inside the jar in the META-INF/manifest.mf file:

Implementation-Version: 3.0.1

If it's less than 3.0 download the 3.0.1 from Maven Central: http://search.maven.org/#artifactdetails|javax.servlet|javax.servlet-api|3.0.1|jar

Former servlet specifications (2.5, 2.4 etc.) do not support annotations.

Upvotes: 17

Dirk
Dirk

Reputation: 1903

You need to add your servlet-api.jar to the build path of your project. Have a look here for how to do that.

Upvotes: -2

Related Questions