I have started to write app that can run on Google App Engine.
But when I wanted to use my code from Netbeans to Eclipse I had an errors on:
import javax.servlet.annotation.WebServlet;
and
#WebServlet(name = "MyServlet", urlPatterns = {"/MyServlet"})
the errors are:
The import javax.servlet.annotation cannot be resolved
WebServlet cannot be resolved to a type
I tried to import the servlet-api.jar to Eclipse but still the same, also tried to build and clean the project. I don't use Tomcat on my Eclipse only have it on my Netbeans. How can I solve the problem?
I tried to import the servlet-api.jar to eclipse but still the same also tried to build and clean the project. I don't use tomcat on my eclipse only have it on my net-beans. How can I solve the problem.
Do not put the servlet-api.jar in your project. This is only asking for trouble. You need to check in the Project Facets section of your project's properties if the Dynamic Web Module facet is set to version 3.0. You also need to ensure that your /WEB-INF/web.xml (if any) is been declared conform Servlet 3.0 spec. I.e. the <web-app> root declaration must match the following:
<web-app
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
In order to be able to import javax.servlet stuff, you need to integrate a fullworthy servletcontainer like Tomcat in Eclipse and then reference it in Targeted Runtimes of the project's properties. You can do the same for Google App Engine.
Once again, do not copy container-specific libraries into webapp project as others suggest. It would make your webapp unexecutabele on production containers of a different make/version. You'll get classpath-related errors/exceptions in all colors.
See also:
How do I import the javax.servlet API in my Eclipse project?
Unrelated to the concrete question: GAE does not support Servlet 3.0. Its underlying Jetty 7.x container supports max Servlet 2.5 only.
Check that the version number of your servlet-api.jar is at least 3.0. There is a version number inside the jar in the META-INF/manifest.mf file:
Implementation-Version: 3.0.1
If it's less than 3.0 download the 3.0.1 from Maven Central: http://search.maven.org/#artifactdetails|javax.servlet|javax.servlet-api|3.0.1|jar
Former servlet specifications (2.5, 2.4 etc.) do not support annotations.
If you're using Maven and don't want to link Tomcat in the Targeted Runtimes in Eclipse, you can simply add the dependency with scope provided in your pom.xml:
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
Go to
window->Preference->server->runtime environment
then choose your tomcat server.
If the error is still there, then
right click project->properties>Targeted Runtimes
then check the server
Simply add the below to your maven project pom.xml flie:
<dependencies>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-web-api</artifactId>
<version>6.0</version>
<scope>provided</scope>
</dependency>
</dependencies>
I had the same issue, it turns that I had my project configured as many of you stated above, yet the problem persisted, so I went deeper and came across that I was using Apache Tomcat 6 as my Runtime Server, so the fix was simple, I just upgraded the project to use Apache Tomcat 7 and that's all, finally the IDE recognized javax.servlet.annotation.WebServlet annotation.
import javax.servlet.annotation.*;
(no one has written this, but need to import this as WebInitparam is not recognized by the other packages)
Copy servlet-api.jar from your tomcat server lib folder.
Paste it to WEB-INF > lib folder
Error was solved!!!
Add library 'Server Runtime' to your java build path, and it shall resolve the issue.
If you are using IBM RAD, then ensure the to remove any j2ee.jar in your projects build path -> libraries tab, and then click on "add external jar" and select the j2ee.jar that is shipped with RAD.
I had the same issue using Spring, solved by adding Tomcat Server Runtime to build path.
You need to add your servlet-api.jar to the build path of your project. Have a look here for how to do that.
Related
Description
When I develop my java project I use:
Java version: 1.8,
Apache Tomcat version 9.0.x
Then, I use Tomcat 10.0.x to run my project (with few modifications in my project). I had to change javax package related imports to jakarta package imports.
When I build the project I got this following error;
java: cannot access javax.servlet.ServletException
class file for javax.servlet.ServletException not found
Fix
When I search for a fix, I found that I need to include Java Servlet API dependency in my pom.xml file. This way I could build the project successfully.
<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
Question
Why do I have to include Java Servlet API dependency when I
migrate to Tomcat 10 ?
I did include Java Servlet API dependency,
but is that a proper fix?
Why do I have to include Java Servlet API dependency when I migrate to Tomcat 10 ?
Presumably because your application has a dependency on something that conforms to the JSP 4.0.x specs.
I did include Java Servlet API dependency, but is that a proper fix?
No it is not the proper fix. While you have succeeded in building your app, you will most get classloader errors when you deploy to Tomcat 10. That's because the Tomcat 10 runtime libraries don't provide the javax.servlet.* classes.
The correct approach is to identify why your application or its dependencies are trying to use classes in javax.servlet ... and eliminate this.
Remove the javax.servlet / javax.servlet-api / 4.0.1 dependency.
Search your application codebase for any remaining references to javax.servlet ... and change them.
Use the Maven dependency tree plugin to try to identify which of your applications dependencies has a dependency on the old javax.servlet API. Look for newer versions of those dependencies that use jakarta.servlet instead.
If 2 and 3 don't identify the culprit, it gets rather difficult. You might need to unpack all of the dependent JAR files and "analyze" them using javap and grep. Or there might be better way.
If you are able to identify the dependencies, but you can't find a Jakarta-compatible version, then you have another kind of problem. You have to choose between putting in the effort to port the dependency, finding another library (or whatever) to provide the functionality, or remove the functionality from your web app.
I want to develop with Servlets in Eclipse, but it says that the package javax.servlet / jakarta.servlet cannot be resolved. How can I add javax.servlet / jakarta.servlet package to my Eclipse project?
Ensure you've the right Eclipse and Server version
Ensure that you're using at least Eclipse IDE for Enterprise Java (and Web) 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, RedHat WildFly, Eclipse GlassFish, 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. Also note that for example WildFly and GlassFish are more than just a servletcontainer, they also support JSF (Faces), EJB (Enterprise Beans), JPA (Persistence) and all other Jakarta 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:
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.
b. Or, via Eclipse preferences
Open Window > Preferences > Server > Runtime Environments.
You can Add, Edit and Remove servers here.
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.
b. Or, in existing project
Rightclick project and choose Properties.
In Targeted Runtimes section, select the integrated server.
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: How to properly configure Jakarta EE libraries in Maven pom.xml for Tomcat?
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
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.
Little bit difference from Hari:
Right click on project ---> Properties ---> Java Build Path ---> Add Library... ---> Server Runtime ---> Apache Tomcat ----> Finish.
Include servlet-api.jar from your server lib folder.
Do this step
Add javax.servlet dependency in pom.xml. Your problem will be resolved.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
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 !!
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.
**OR**
you can directly add jar files to library by using following steps.
Right click on project.
Go To Properties.
Go to Java Build Path.
Select Add Library option from tabs.
Add Jar Files
give path of your servlet-api.jar file.
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.
For maven projects add following dependancy :
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
Reference
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.
From wikipedia.
import java.io.IOException;
import java.io.PrintWriter;
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" +
"</body></html>");
}
}
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.
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.
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.
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.
This could be also the reason. i have come up with following pom.xml.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
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.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>compile</scope>
</dependency>
You should above all never manually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar
#BalusC,
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.
I want to develop with Servlets in Eclipse, but it says that the package javax.servlet / jakarta.servlet cannot be resolved. How can I add javax.servlet / jakarta.servlet package to my Eclipse project?
Ensure you've the right Eclipse and Server version
Ensure that you're using at least Eclipse IDE for Enterprise Java (and Web) 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, RedHat WildFly, Eclipse GlassFish, 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. Also note that for example WildFly and GlassFish are more than just a servletcontainer, they also support JSF (Faces), EJB (Enterprise Beans), JPA (Persistence) and all other Jakarta 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:
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.
b. Or, via Eclipse preferences
Open Window > Preferences > Server > Runtime Environments.
You can Add, Edit and Remove servers here.
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.
b. Or, in existing project
Rightclick project and choose Properties.
In Targeted Runtimes section, select the integrated server.
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: How to properly configure Jakarta EE libraries in Maven pom.xml for Tomcat?
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
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.
Little bit difference from Hari:
Right click on project ---> Properties ---> Java Build Path ---> Add Library... ---> Server Runtime ---> Apache Tomcat ----> Finish.
Include servlet-api.jar from your server lib folder.
Do this step
Add javax.servlet dependency in pom.xml. Your problem will be resolved.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
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 !!
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.
**OR**
you can directly add jar files to library by using following steps.
Right click on project.
Go To Properties.
Go to Java Build Path.
Select Add Library option from tabs.
Add Jar Files
give path of your servlet-api.jar file.
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.
For maven projects add following dependancy :
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
Reference
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.
From wikipedia.
import java.io.IOException;
import java.io.PrintWriter;
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" +
"</body></html>");
}
}
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.
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.
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.
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.
This could be also the reason. i have come up with following pom.xml.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
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.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>compile</scope>
</dependency>
You should above all never manually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar
#BalusC,
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.
I want to develop with Servlets in Eclipse, but it says that the package javax.servlet / jakarta.servlet cannot be resolved. How can I add javax.servlet / jakarta.servlet package to my Eclipse project?
Ensure you've the right Eclipse and Server version
Ensure that you're using at least Eclipse IDE for Enterprise Java (and Web) 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, RedHat WildFly, Eclipse GlassFish, 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. Also note that for example WildFly and GlassFish are more than just a servletcontainer, they also support JSF (Faces), EJB (Enterprise Beans), JPA (Persistence) and all other Jakarta 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:
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.
b. Or, via Eclipse preferences
Open Window > Preferences > Server > Runtime Environments.
You can Add, Edit and Remove servers here.
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.
b. Or, in existing project
Rightclick project and choose Properties.
In Targeted Runtimes section, select the integrated server.
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: How to properly configure Jakarta EE libraries in Maven pom.xml for Tomcat?
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
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.
Little bit difference from Hari:
Right click on project ---> Properties ---> Java Build Path ---> Add Library... ---> Server Runtime ---> Apache Tomcat ----> Finish.
Include servlet-api.jar from your server lib folder.
Do this step
Add javax.servlet dependency in pom.xml. Your problem will be resolved.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
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 !!
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.
**OR**
you can directly add jar files to library by using following steps.
Right click on project.
Go To Properties.
Go to Java Build Path.
Select Add Library option from tabs.
Add Jar Files
give path of your servlet-api.jar file.
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.
For maven projects add following dependancy :
<!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
Reference
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.
From wikipedia.
import java.io.IOException;
import java.io.PrintWriter;
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" +
"</body></html>");
}
}
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.
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.
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.
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.
This could be also the reason. i have come up with following pom.xml.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
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.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>compile</scope>
</dependency>
You should above all never manually copy/download/move/include the individual servletcontainer-specific libraries like servlet-api.jar
#BalusC,
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.
I'm building a web application with Eclipse using Maven. The server is going to be Apache Tomcat. Eclipse already has tomcat 6 libraries which you can include in your build path and Web Application Module facet to be chosen. That's the way I work without Maven.
However, Maven is able to include the required dependencies to use them in tomcat. My question is, what is the right thing, not to include them via Maven and continue doing that like before, or not to configure eclipse build path and make maven solve it?
The best approach for container specific API like the server API is include it in the maven POM however set the dependency as provided scope so it will be available in your class path for eclipse however maven will not package it in the WAR file when generating it. e.G.
<dependency>
<groupId>...</groupId>
<artifactId>...</artifactId>
<version>...</version>
<scope>provided</scope>
</dependency>
As far as I know Maven do not solve it, if you do not specifically say that your project needs these dependences. The best way is to configure the pom.xml and set the dependencies and build the project using Maven and edit it in eclipse. So once you are done you can easily "package" (build the war) and deploy it in any server location.