How do I import the javax.servlet API in my Eclipse project?

Ensure that you’re using at least Eclipse IDE for Java EE developers (with the EE). It contains development tools to create dynamic web projects and easily integrate servletcontainers (which is part of Web Tools Platform, WTP). You also need to ensure that you already have a servletcontainer installed which implements at least the same Servlet API as the servletcontainer in the production environment, for example Apache Tomcat, Oracle Glassfish, JBoss AS, etc.

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 already have Glassfish. Also note that for example Glassfish and JBoss AS are more than just a servletcontainer, they also supports JSF, EJB, JPA and all other Java EE fanciness.


Once having installed both, do the following steps in Eclipse:


Integrate servletcontainer in Eclipse. 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.


Create new dynamic web project in Eclipse which is associated with the integrated servletcontainer. Open the Project Navigator on the left hand side. Rightclick there and choose New > Project and then in menu Web > Dynamic Web Project and set the Target Runtime to the integrated server. 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.

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.


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