Can't import javax.servlet.annotation.WebServlet
Solution 1
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.
Solution 2
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.
Solution 3
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>
Solution 4
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
Solution 5
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>
![Vitaly Menchikovsky](https://i.stack.imgur.com/hvZXx.jpg?s=256&g=1)
Vitaly Menchikovsky
By day: worked at Amdocs for 2 years as FE & BE Dev(JAVA,Backbone...), Now working at Gala coral as a FE dev with Backbone. Know also JS,Angular,redux,react, ionic, Java, Bootstrup,auto testing cucumber,firebase. Hobbies: know LR, photo Shop, and professional photography.
Updated on August 21, 2020Comments
-
Vitaly Menchikovsky almost 4 years
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? -
Achow over 11 yearsShould never be done, and it won't solve the problem anyways.
-
Achow over 11 yearsDownvoter, can u please explain what's wrong with my comment above? that u downvoted it?
-
Dirk over 11 yearsNo idea why this was downvoted and also what @anirban is "talking" about almost 1.5 years after this question is answered and accepted ...
-
Achow over 11 yearsEven though the correct answer was accepted 1.5 years, this answer is conceptually incorrect and since its "easy", it might tempt developers to use it, but once they try this out,it wont work out.Its quite possible that the developer would NOT remove this dependency and would only look for correct answers.End result : the project gets doomed with 1 incorrect classpath dependency.
-
Abel Callejo over 10 yearshow can one integrate Tomcat in Eclipse?
-
BalusC over 10 years@Abel: click the "See also" link.
-
Chris about 4 yearsmeanwhile version 8.0.1 :)