eclipse 为什么导入 javax.servlet.* 会出错?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5214143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 15:55:28  来源:igfitidea点击:

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

javaeclipsemaven-2maven

提问by user496949

I am using JavaSE6 and Eclipse, there is an error in the line

我正在使用 JavaSE6 和 Eclipse,行中有错误

import javax.servlet.*

It seems there is no jar for this import.

似乎没有用于此导入的 jar。

How to fix it? Install anything, use Eclipse EE or add some dependency in Maven?

如何解决?安装任何东西,使用 Eclipse EE 还是在 Maven 中添加一些依赖项?

回答by Martin

The servlet API is not a part of the JDK, you need to add an additional dependency to your pom.xml.

servlet API 不是 JDK 的一部分,您需要向pom.xml.

If this is for a webapp you can add this dependency with providedscope and the servlet container will make these classes available to your webapp at deployment time.

如果这是用于 web 应用程序,您可以添加此依赖项和provided范围,servlet 容器将在部署时使这些类可用于您的 web 应用程序。

<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>servlet-api</artifactId>
  <version>2.5</version>
  <scope>provided</scope>
</dependency>

回答by Buhake Sindi

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.

javax.servlet.*是 Servlet API(Java EE 框架的一部分)的一部分。希望使用 Servlet 的 Web 应用程序服务器/Web 容器必须实现 Servlet API。

Tomcat has servlet-api.jarwhich 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.

Tomcatservlet-api.jar可以在下面找到TOMCAT_HOME/lib(在 Tomcat 6 和更高版本中)。根据您正在运行的 Web 应用程序服务器找到与您相关的一个。

回答by Peter C

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

javax.servlet仅适用于 Java 企业版。要么使用它,要么获取必要的 JAR 文件(我使用Apache Tomcat获取它们)。

回答by Matt Ball

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...

你需要servlet-api.jar. 假设您使用的是 Eclipse,您可以通过右键单击项目 →属性 → Java 构建路径 → 添加外部 JAR来将 jar 添加到构建路径中...

If you're using Tomcat 5.5, the JAR is in $CATALINA_HOME/common/lib.

如果您使用的是Tomcat 5.5,则 JAR 位于$CATALINA_HOME/common/lib.

If you're using Tomcat 6+, the JAR is in $CATALINA_HOME/lib.

如果您使用的是Tomcat 6+,则 JAR 位于$CATALINA_HOME/lib.

If you're using JBoss 5, the JAR is in$JBOSS_HOME/common/lib.

如果您使用的是JBoss 5,则 JAR 位于$JBOSS_HOME/common/lib.

回答by adarshr

Tomcat? You need the servlet-api.jarpresent inside tomcat/lib

雄猫?你需要servlet-api.jar里面的礼物tomcat/lib