Accepted answer

ensure you've the right eclipse and server version

ensure that you're using at least eclipse ide for enterprise java developers (with the enterprise). it contains development tools to create dynamic web projects and easily integrate servletcontainers (those tools are part of web tools platform, wtp). in case you already had eclipse ide for java (without enterprise), and manually installed some related plugins, then chances are that it wasn't done properly. you'd best trash it and grab the real eclipse ide for enterprise java one.

you also need to ensure that you already have a servletcontainer installed on your machine which implements at least the same servlet api version as the servletcontainer in the production environment, for example apache tomcat, oracle glassfish, jboss as/wildfly, etc. usually, just downloading the zip file and extracting it is sufficient. in case of tomcat, do not download the exe format, that's only for windows based production environments. see also a.o. several ports (8005, 8080, 8009) required by tomcat server at localhost are already in use.

a servletcontainer is a concrete implementation of the servlet api. note that the java ee sdk download at basically contains glassfish. so if you happen to already have downloaded java ee sdk, then you basically already have glassfish. also note that for example glassfish and jboss as/wildfly are more than just a servletcontainer, they also supports jsf, ejb, jpa and all other java ee fanciness. see also a.o. what exactly is java ee?

ensure that you're using the right servlet package

the javax.* package has been renamed to jakarta.* package since servlet api version 5.0 which is part of jakarta ee 9 (tomcat 10, tomee 9, wildfly 22 preview, glassfish 6, payara 6, liberty 22, etc). so if you're targeting these server versions or newer, then you need to replace

import javax.servlet.*;
import javax.servlet.http.*;


import jakarta.servlet.*;
import jakarta.servlet.http.*;

in order to get it to compile, else you might risk to face this build error

the superclass "javax.servlet.http.httpservlet" was not found on the java build path

integrate server in eclipse and associate it with project

once having installed both eclipse for enterprise java and a servletcontainer on your machine, do the following steps in eclipse:

  1. integrate servletcontainer in eclipse

    a. via servers view

    • open the servers view in the bottom box.

    • rightclick there and choose new > server.

    • pick the appropriate servletcontainer make and version and walk through the wizard.

      enter image description here

    b. or, via eclipse preferences

    • open window > preferences > server > runtime environments.

    • you can add, edit and remove servers here.

      enter image description here

  2. associate server with project

    a. in new project

    • open the project navigator/explorer on the left hand side.

    • rightclick there and choose new > project and then in menu web > dynamic web project.

    • in the wizard, set the target runtime to the integrated server.

      enter image description here

    b. or, in existing project

    • rightclick project and choose properties.

    • in targeted runtimes section, select the integrated server.

      enter image description here

    either way, eclipse will then automatically take the servletcontainer's libraries in the build path. this way you'll be able to import and use the servlet api.

never carry around loose server-specific jar files

you should in any case not have the need to fiddle around in the build path property of the project. you should above all never manually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar, jsp-api.jar, el-api.jar, j2ee.jar, javaee.jar, etc. it would only lead to future portability, compatibility, classpath and maintainability troubles, because your webapp would not work when it's deployed to a servletcontainer of a different make/version than where those libraries are originally obtained from.

in case you're using maven, you need to make absolutely sure that servletcontainer-specific libraries which are already provided by the target runtime are marked as <scope>provided</scope>. you can find examples of proper pom.xml dependency declarations for tomcat 10+, tomcat 9-, jee 9+ and jee 8- in this answer: tomcat 9 casting servlets to javax.servlet.servlet instead of jakarta.servlet.http.httpservlet

here are some typical exceptions which you can get when you litter the /web-inf/lib or even /jre/lib, /jre/lib/ext, etc with servletcontainer-specific libraries in a careless attempt to fix the compilation errors:


you should above all never manually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar


i would prefer to use the exact classes that my application is going to use rather than one provided by eclipse (when i am feeling like a paranoid developer).

another solution would be to use eclipse "configure build path" > libraries > add external jars, and add servlet api of whatever container one chooses to use.

and follow @kaustav datta's solution when using ant to build - have a property like tomcat.home or weblogic.home. however it introduces another constraint that the developer must install weblogic on his/her local machine if weblogic is being used ! any other cleaner solution?


assume a simple dynamic web project. ensure tomcat is present on the project's build path.

first get the local tomcat instance downloaded/installed on your machine. assume we installed tomcat v8.5.

now configure the eclipse global server run time environment

windows > preferences > server > runtime environments > add > apache > apache tomcat v8.5 > next > browse tomcat installation directory > next > finish.

now create your dynamic web project and we see the import javax.servlet cannot be resolved issue. resolve this by following below.

right click on project > build path > configure build path > libraries > add library > server runtime > select your apache tomcat instance > finish > apply and close.

all errors will be gone.


this could be also the reason. i have come up with following pom.xml.


the unresolved issue was due to exclusion of spring-boot-starter-tomcat. just remove <exclusions>...</exclusions> dependency it will ressolve issue, but make sure doing this will also exclude the embedded tomcat server.

if you need embedded tomcat server too you can add same dependency with compile scope.



i was getting a null pointer exception during project creation related to "dynamic web module".

to get the project to compile (that is, to javax.servlet to import successfully) i had to go to project's properties, pick project facets in the sidebar, tick dynamic web module and click apply.

surprisingly, this time "dynamic web module" facet installed correctly, and import started to work.


many of us develop in eclipse via a maven project. if so, you can include tomcat dependencies in maven via the tomcat-servlet-api and tomcat-jsp-api jars. one exists for each version of tomcat. usually adding these with scope provided to your pom is sufficient. this will keep your build more portable.

if you upgrade tomcat in the future, you simply update the version of these jars as well.


from wikipedia.


import javax.servlet.servletexception;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

public class helloworld extends httpservlet {
  public void doget(httpservletrequest request, httpservletresponse response)
      throws servletexception, ioexception {
    printwriter out = response.getwriter();
    out.println("<!doctype html public \"-//w3c//dtd html 4.0 " +
                "transitional//en\">\n" +
                "<html>\n" +
                "<head><title>hello www</title></head>\n" +
                "<body>\n" +
                "<h1>hello www</h1>\n" +

this, of course, works only if you have added the servlet-api.jar to eclipse build path. typically your application server (e.g tomcat) will have the right jar file.


in my case, when i went to the targetted runtimes, screen, tomcat 7 was not listed (disabled) despite being installed.

to fix, i had to go to preferences->server->runtime environments then uninstall and reinstall tomcat 7.


for maven projects add following dependancy :

<!-- -->


for gradle projects:

dependencies {
providedcompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'

or download javax.servlet.jar and add to your project.


you can simply copy the servlet-api.jar and copy that jar files into lib folder, which is in web-inf. then just clean and built your project, your errors will be solved.


you can directly add jar files to library by using following steps.

  1. right click on project.
  2. go to properties.
  3. go to java build path.
  4. select add library option from tabs.
  5. add jar files
  6. give path of your servlet-api.jar file.
  7. clean and build your project.


i know this is an old post. however, i observed another instance where in the project already has tomcat added but we still get this error. did this to resolve that:
alt + enter
project facets
on the right, next to details, is another tab "runtimes". the installed tomcat server will be listed there. select it.
save the configuration and done!

hope this helps someone.


quick fix- this worked in eclipse - right click on project -> properties -> java build path (tab) -> add external jars -> locate the servlet api jar implementation (if tomcat - its named servlet-api.jar) -> click ok. that's it !!


include servlet-api.jar from your server lib folder.enter image description here

do this step

enter image description here


add javax.servlet dependency in pom.xml. your problem will be resolved.



little bit difference from hari:

right click on project ---> properties ---> java build path ---> add library... ---> server runtime ---> apache tomcat ----> finish.


import javax.servlet

step 1

go to properties of your project ( with alt+enter or righ-click )

step 2

check on apache tomcat v7.0 under targeted runtime and it works.

Related Query

More Query from same tag