简体   繁体   中英

Why there is an error in import javax.servlet.*?

I am using JavaSE6 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.

javax.servlet is only available with Java Enterprise Edition. Either use it, or acquire the necessary JAR files (I got them with Apache Tomcat ).

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 .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM