When you start to develop web application using Java, you may think to use Eclipse as the IDE. Assume that you have seen a good tutorial about Java servlet on the internet or read the chapter of a book discussing about web development using JSP.
You are now writing the first servlet by using the wizard provided by the IDE. However, as soon the template file is loaded on the source code editor, you encounter the infamous error “The import javax.servlet cannot be resolved” just like depicted in the figure below.
This error is normal. Servlet library is not shipped by default in Eclipse. You also can’t find the library in JRE/JDK package you download. Instead, you will find it in the lib folder under Tomcat installation directory like depicted below.
So, how can we resolve the error? The solution is apparently simple. We just need to add the servlet jar file into the build path. Based on the scope, we can achieve this using two methods; single project-wise and all projects-wise. Both methods will be explained immediately. You just need to pay attention that these methods were tested under the following system environment and steps in other environment may vary:
OS: Windows
Eclipse version: Eclipse 3.5 Ganymede
JDK version: JDK 1.6.0_14
In the first method, we add the servlet-api jar by right-clicking the project name and choose Properties option. After clicking, a new popup window will appear. Click the “Java Build Path” menu on the left followed by choosing “Libraries” tab. Subsequently, click “Add External JARs” button and point to servlet-api.jar in lib folder of Apache Tomcat installation directory. If you do all these steps correctly, you will notice that servlet-api.jar has been added into the libraries. Please check your result with the reference picture below.
Click “OK” to complete the modification. After a glance, you will notice all imports errors have been disappeared.
The first method is good if you rarely work with servlet. If you yourself are a very busy developer who has to build numerous projects for your vast client base, it is considerably wise to apply the second method. In this method, we will add the servlet-api.jar into the JDK directory. After the modification, you will never encounter the error again as the servlet-api.jar will always be in the build path. This is different with the previous method which requires you to modify the project properties again if you create a new dynamic web project.
To execute the second method, follow the instructions below:
1. In Eclipse workbench, choose Window > Preferences
2. Choose Java > Installed JREs on the left menu
3. Focus on the JDK you want to modify, click “Edit”. A new popup will appear
4. Click “Add External JARs” and point to servlet-api.jar in lib folder of Tomcat installation directory.
5. Click “Finish” to close the second popup and “OK” to complete the modification
After you do all the steps, all the servlet import errors in the source code editor will have vanished. You will also have discarded the import error for all projects which require the servlet functionalities.
Thanks!!!))
Excellent Bro!! Very Clear, thanks
Thanks !
It is very helpful
@all
Thanks. Nice to know it helps 🙂
THANKS A LOT..I WAS TRYING TO UNINSTALL ECLIPSE..
The first one works fine for me, but the second gives several Java Problems message like
“Access restriction: The type HttpServletResponse is not accessible due to restriction on required library C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar”
I don’t know these are errors or warnings, anyway my program can still run as it should be.
My system:
“JRE 1.6 update 22”
“Eclipse Java EE IDE for Web Developers.
Version: Helios Service Release 1
Build id: 20100917-0705”
Thanks! I will go on.
This helped me. Thanks!
@Jass
make sure it was a copy-and-paste process instead of a cut-and-paste. also ensure that tomcat instance is not running when you invoke the operation.
wonderful thank you!
Thankyou somuch it helps me a lot
Thank you SO much for publishing the solution to this problem! I just put eclipse on a new computer and couldn’t figure out the problem.
Thanks. The problem began after I uninstalled Tomcat 😛
Thanks a lot
Wow, i was having a headache with this problem and now you have fixed it, thanks.
Thanks…it worked..:)
really helpful…. thank you …
Thanks… Exactly what I need.. 🙂
Thanks for your explain.
Also If you have added tomcat server to eclipse already, you can click on add library and select tomcat from Server Run-time section. and you don’t need to import it from external jar file yourself.
Thanks, take it
Thanks !
All errors disappeared except the one for
“import javax.servlet.annotation.WebServlet;”
I’m using Eclipse 3.7.0 on Mac OSX 10.6.7 and imported lib from Tomcat 6
Hey rosh,
simply add below properties of the project a new runtime containing tomcat 7 as target platform and select it. After refreshing the project and moving to libraries you will see the jar and you can compile your webapp including annotations like @WebServlet.
Thank you very much. It worked
@rosh
these days i prefer using maven to manage the building configuration. using this approach, such kind of library dependencies will be easier to be solved. i will come with posts about maven later.
This one goes to bookmarks! 😉
Thanks a lot
Thank you 🙂
Thank Dear,
I got the result.I was trying to copy the servlet-api.jar file to lib directory of WEB-INF.
Thanks once again.
THANKS A LOT .I FOUND THIS PROBLEM BUT YOU SAVED ME FROM GOING TO TROUBLE
@Thanks.. !!!!
“Access restriction: The type HttpServletResponse is not accessible due to restriction on required library C:\Program Files\Apache Software Foundation\Tomcat 6.0\lib\servlet-api.jar”
On the Java Build Path Libraires tab open your .jar to see the Access rules.
Edit…
Add…
Resolution: Accessible
Rule Pattern:*
Thanks mate. Excellent work and thanks for sharing knowledge with others.
Thaks a lot..!
Thaks a lot..!
thanx …
thanks…..
GREAT!!!! very good step by step explanation. It was a great help. Thank you.
Thanks a lot.. It helped me…
Thanks!!!!!
It’s working,thanks alot.
but i’m still facing the problem..
it is still showing problem importing servlet..
please resolve it
Helped a lot! Thanks!!
Thanks a lot!!!
thanks a lot …
thanks alot bro…really a helpful information
I’m still having the problem. I’m trying to use these classes:
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;
but having the error “The import jawax.ws can not be resolved”
I’m still having the problem. I’m trying to use these classes:
import javax.ws.rs.Consumes;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.Path;
but having the error “The import jawax.ws can not be resolved”.
Pleaze help me. Thanks in advance.
hi shirish,
javax.ws.rs is not a part of java servlet specifications. it’s the api implementation of JSR-311 (Java API for RESTful web service). You can search the net with keyword “jsr311-api jar” or use dependency management like maven to handle this dependency. i will come with some articles about maven soon.
thanks alot
Thanks alot.
Thankyou Thankyou Thankyou!!!