How to Import the Javax.Servlet/Jakarta.Servlet API in My Eclipse Project

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

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 Oracle.com 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.*;

by

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.

      Sample Image

    b. Or, via Eclipse preferences

    • Open Window > Preferences > Server > Runtime Environments.

    • You can Add, Edit and Remove servers here.

      Sample Image

  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.

      Sample Image

    b. Or, in existing project

    • Rightclick project and choose Properties.

    • In Targeted Runtimes section, select the integrated server.

      Sample Image

    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:

  • java.lang.NullPointerException at org.apache.jsp.index_jsp._jspInit
  • java.lang.NoClassDefFoundError: javax/el/ELResolver
  • java.lang.NoSuchFieldError: IS_DIR
  • java.lang.NoSuchMethodError: javax.servlet.jsp.PageContext.getELContext()Ljavax/el/ELContext;
  • java.lang.AbstractMethodError: javax.servlet.jsp.JspFactory.getJspApplicationContext(Ljavax/servlet/ServletContext;)Ljavax/servlet/jsp/JspApplicationContext;
  • org.apache.jasper.JasperException: The method getJspApplicationContext(ServletContext) is undefined for the type JspFactory
  • java.lang.VerifyError: (class: org/apache/jasper/runtime/JspApplicationContextImpl, method: createELResolver signature: ()Ljavax/el/ELResolver;) Incompatible argument to function
  • jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class

Eclipse: How do I add the javax.servlet package to a project?

To expound on darioo's answer with a concrete example. Tomcat 7 installed using homebrew on OS X, using Eclipse:

  1. Right click your project folder, select Properties at the bottom of the context menu.
  2. Select "Java Build Path"
  3. Click Libraries" tab
  4. Click "Add Library..." button on right (about halfway down)
  5. Select "Server Runtime" click "Next"
  6. Select your Tomcat version from the list
  7. Click Finish

What? No Tomcat version is listed even though you have it installed via homebrew??

  1. Switch to the Java EE perspective (top right)
  2. In the "Window" menu select "Show View" -> "Servers"
  3. In the Servers tab (typically at bottom) right click and select "New > Server"
  4. Add the path to the homebrew tomcat installation in the dialog/wizard (something like: /usr/local/Cellar/tomcat/7.0.14/libexec)

Hope that helps someone who is just getting started out a little.

The import javax.servlet can't be resolved

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.

The import javax.servlet cannot be resolved STILL

jakarta.servlet versus javax.servlet

Any chance someone has a simple, factual answer as to why this still doesn't work?

Because:

  • You are building against the Tomcat 10.0.10 servlet-api JAR file.
  • Tomcat 10.0.x implements version 5.0 of the Servlet spec; see http://tomcat.apache.org/whichversion.html
  • Servlet 5.0 is Jakarta EE, not Java EE; see https://en.wikipedia.org/wiki/Jakarta_Servlet#History
  • In later versions of Jakarta EE, the package names for the servlet classes have changed from javax.servlet to jakarta.servlet.

And your code is trying to use the old package name.

Solutions:

  1. Change your webapp code to import from the new jakarta.servlet package, OR
  2. Roll back to a version of Tomcat that supports the older version of the Servlet spec; i.e. Tomcat 9.0.x or earlier.

Eclipse complains about jakarta.servlet.http.HttpServlet was not found

can I stick with javax and not jakarta?

Yes, certainly you can stick with the javax. naming rather than migrating to the new jakarta. namespace. Eventually you’ll want to make the migration to benefit from new and improved technologies. But not necessary this year or next.

Read the Which version? documentation page. You will see that versions 9.0.x and 10.0.x are functionally equivalent, developed in parallel. The only significant difference is that namespace change discussed above. So use Tomcat 9 if you choose to stick with javax naming.

You’ll need to get your codebase in order, to use only the javax. naming. Apparently you have some code that refers to the new jakarta. naming. Fix that. Check your import statements. Check any use of fully-qualified class names. Use your IDE’s search tools.

By the way… If using Tomcat 9, you can change that dependency on the Servlet API from 3.0.1 to the version 4.0.3 of the Servlet specification. See that Which version? page linked above, as well as the https://Jakarta.EE site, to learn the appropriate versions of specs for JSPs, etc.

Why does import javax.servlet.*; cannot be resolved despite of installing Java EE | Eclipse for Java EE Developers

please cross check if your eclipse is pointing to JDK and not JRE

check in build path, it has to point to JDK change if it is pointing to JRE
Like this



Related Topics



Leave a reply



Submit