[java] 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?

This question is related to java eclipse servlets servlet-3.0

The answer is


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.


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.


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.


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>

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>

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


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


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


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


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


import javax.servlet.annotation.*;

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


Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to eclipse

How do I get the command-line for an Eclipse run configuration? My eclipse won't open, i download the bundle pack it keeps saying error log strange error in my Animation Drawable How to uninstall Eclipse? How to resolve Unable to load authentication plugin 'caching_sha2_password' issue Class has been compiled by a more recent version of the Java Environment Eclipse No tests found using JUnit 5 caused by NoClassDefFoundError for LauncherFactory How to downgrade Java from 9 to 8 on a MACOS. Eclipse is not running with Java 9 "The POM for ... is missing, no dependency information available" even though it exists in Maven Repository The origin server did not find a current representation for the target resource or is not willing to disclose that one exists. on deploying to tomcat

Examples related to servlets

Google Recaptcha v3 example demo Difference between request.getSession() and request.getSession(true) init-param and context-param java.lang.NoClassDefFoundError: org/json/JSONObject how to fix Cannot call sendRedirect() after the response has been committed? getting error HTTP Status 405 - HTTP method GET is not supported by this URL but not used `get` ever? Create a simple Login page using eclipse and mysql Spring get current ApplicationContext insert data into database using servlet and jsp in eclipse What is WEB-INF used for in a Java EE web application?

Examples related to servlet-3.0

What is difference between @RequestBody and @RequestParam? The server encountered an internal error that prevented it from fulfilling this request - in servlet 3.0 Recommended way to save uploaded files in a servlet application How to set up JAX-RS Application using annotations only (no web.xml)? Can't import javax.servlet.annotation.WebServlet