The import javax.servlet can't be resolved

snakile picture snakile · Nov 7, 2010 · Viewed 362k times · Source

I'm trying to use eclipse for Java EE to develop web applications.

I need to use Tomcat as my server. I've downloaded Tomcat and it's running. But my program doesn't compile.

I get the following error:

The import javax.servlet can't be resolved.

What do I need to do?

Answer

Richard Fearn picture Richard Fearn · Nov 7, 2010

You need to add the Servlet API to your classpath. In Tomcat 6.0, this is in a JAR called servlet-api.jar in Tomcat's lib folder. You can either add a reference to that JAR to the project's classpath, or put a copy of the JAR in your Eclipse project and add it to the classpath from there.

If you want to leave the JAR in Tomcat's lib folder:

  • Right-click the project, click Properties.
  • Choose Java Build Path.
  • Click the Libraries tab
  • Click Add External JARs...
  • Browse to find servlet-api.jar and select it.
  • Click OK to update the build path.

Or, if you copy the JAR into your project:

  • Right-click the project, click Properties.
  • Choose Java Build Path.
  • Click Add JARs...
  • Find servlet-api.jar in your project and select it.
  • Click OK to update the build path.