Eclipse Quick Tip: Resolving Error “The import javax.servlet cannot be resolved”

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.

Servlet import error

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.

Servlet API jar file

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.

Adding servlet API into project library

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

Adding servlet API JAR file into universal build path

4. Click “Add External JARs” and point to servlet-api.jar in lib folder of Tomcat installation directory.

Adding Servlet API JAR file into universal build path-2

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.

101 thoughts on “Eclipse Quick Tip: Resolving Error “The import javax.servlet cannot be resolved”

  1. Ken

    Another way to solve this problem is to add tomcat in your preference and select it as your default server. This ensures that the servlet api would be available. This also eliminates the import issue.

  2. Dave L.

    Thanks for saving me a lot of head scratching. Not a java developer by trade so relying on tips like this to help me get this darn project working!

  3. Saurabh

    Seems good among all the solution seen so far…Hope it works…and thanks in advance…bookmarking U…!!!

  4. adigayo

    Just in case you’re using Glassfish 4.0 Server,
    try these steps :
    – right-click project > properties > Project Facets > Glassfish Web Extention > Runtime > checklist GlassFish Instance (eg : GlassFish 4.0 at Localhost)
    – click Apply
    It works fine..

  5. Raghavendra Rao PV

    First I thought this is non-sense, but it worked 🙂

    You do this by setting a Targeted Runtime. This is set in the Project properties. In Eclipse, go to Project -> Properties, then click on the Targeted Runtimes page. On that page, you can select the runtime (application server) your application will use.

    1. Mikael Fernandus Simalango Post author

      Good to know the workaround still works in 2022. At the same time, it is unfortunate that Eclipse users still face the same issue 12 years after the article was published.

  6. Pingback: ¿Dónde está el servlet-api.jar en Glassfish 5.1? - servlets en Español

Leave a Reply

Your email address will not be published. Required fields are marked *