Why there is an error in import javax.servlet.*?
I am using Java开发者_C百科SE6 and Eclipse, there is an error in the line
import javax.servlet.*
It seems there is no jar for this import.
How to fix it? Install anything, use Eclipse EE or add some dependency in Maven?
The servlet API is not a part of the JDK, you need to add an additional dependency to your pom.xml
.
If this is for a webapp you can add this dependency with provided
scope and the servlet container will make these classes available to your webapp at deployment time.
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
javax.servlet.*
is part of the Servlet API (which is part of the Java EE framework). Web Application Server/Web Containers wishing to use Servlets must implement the Servlet API.
Tomcat has servlet-api.jar
which can be found under TOMCAT_HOME/lib
(in Tomcat 6 and higher).
Find one that's relevant to you based on the Web Application Server you're running.
You need servlet-api.jar
. Assuming you're using Eclipse, you'd add the jar to your build path by right-clicking the project → Properties → Java Build Path → Add External JARs...
If you're using Tomcat 5.5, the JAR is in $CATALINA_HOME/common/lib
.
If you're using Tomcat 6+, the JAR is in $CATALINA_HOME/lib
.
If you're using JBoss 5, the JAR is in$JBOSS_HOME/common/lib
.
javax.servlet
is only available with Java Enterprise Edition. Either use it, or acquire the necessary JAR files (I got them with Apache Tomcat).
Tomcat? You need the servlet-api.jar
present inside tomcat/lib
If you are using Tomcat 10 with jdk 11 or later version
if your tomcat server is running but you are getting error with javax.servlet.HttpServlet
error.
Then just modify javax to jakarta.servlet.HttpServlet
.
精彩评论