我想在Eclipse中使用servlet进行开发,但它说包javax。Servlet / jakarta。Servlet无法解析。如何添加javax。Servlet / jakarta。servlet包到我的Eclipse项目?


当前回答

确保您拥有正确的Eclipse和Server版本

确保您至少为企业Java(和Web)开发人员(使用企业版)使用Eclipse IDE。它包含了用于创建动态web项目和轻松集成servlet容器的开发工具(这些工具是web工具平台WTP的一部分)。如果您已经有了用于Java的Eclipse IDE(没有Enterprise),并手动安装了一些相关的插件,那么很可能没有正确地完成。您最好丢弃它,并为Enterprise Java one使用真正的Eclipse IDE。

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.

Servlet容器是Servlet API的具体实现。还要注意,例如WildFly和GlassFish不仅仅是一个servlet容器,它们还支持JSF (Faces)、EJB (Enterprise Beans)、JPA (Persistence)和所有其他Jakarta EE的功能。请参见a.o。Java EE到底是什么?

确保使用了正确的Servlet包

javax。*包已重命名为jakarta。*从Servlet API 5.0版本开始包,它是Jakarta EE 9的一部分(Tomcat 10, TomEE 9, WildFly 22预览,GlassFish 6, Payara 6, Liberty 22等)。因此,如果您的目标是这些服务器版本或更新版本,则需要进行替换

import javax.servlet.*;
import javax.servlet.http.*;

by

import jakarta.servlet.*;
import jakarta.servlet.http.*;

为了让它编译,否则您可能会面临这个构建错误的风险

超类“javax.servlet.http”。在Java构建路径中找不到HttpServlet

在Eclipse中集成服务器并将其与项目相关联

在您的机器上安装了Eclipse for Enterprise Java和servlet容器之后,在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.

永远不要携带松散的特定于服务器的JAR文件

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.

在使用Maven的情况下,需要绝对确保已经由目标运行时提供的特定于servletcontainer的库被标记为<scope>provided</scope>。你可以在这个回答中找到Tomcat 10+、Tomcat 9-、JEE 9+和JEE 8-的正确pom.xml依赖声明的例子:如何在Maven pom.xml中为Tomcat正确配置Jakarta EE库?

以下是一些典型的异常,当你在/WEB-INF/lib甚至/JRE/lib, /JRE/lib/ext等特定于servletcontainer的库中粗心地试图修复编译错误时,你会得到:

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

其他回答

对于专业项目,添加以下依赖项:

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

参考

对于gradle项目:

dependencies {
providedCompile group: 'javax.servlet', name: 'javax.servlet-api', version: '3.0.1'
}

或者下载javax.servlet.jar并添加到您的项目中。

快速修复-这在Eclipse中工作-右键单击项目->属性-> Java构建路径(选项卡)->添加外部jar ->定位servlet api jar实现(如果Tomcat -其命名为servlet-api.jar) ->单击确定。就是这样!!

步骤1

转到项目属性(用Alt+Enter或右键单击)

步骤2

在目标运行时下检查Apache Tomcat v7.0,它可以工作。

我知道这是一个老帖子。然而,我观察到另一个实例,在该项目中已经添加了Tomcat,但我们仍然得到这个错误。这样做是为了解决: Alt + Enter 项目方面 在右边,在详细信息旁边,是另一个选项卡“运行时间”。 已安装的tomcat服务器将列在那里。选择它。 保存配置并完成!

希望这能帮助到一些人。

确保您拥有正确的Eclipse和Server版本

确保您至少为企业Java(和Web)开发人员(使用企业版)使用Eclipse IDE。它包含了用于创建动态web项目和轻松集成servlet容器的开发工具(这些工具是web工具平台WTP的一部分)。如果您已经有了用于Java的Eclipse IDE(没有Enterprise),并手动安装了一些相关的插件,那么很可能没有正确地完成。您最好丢弃它,并为Enterprise Java one使用真正的Eclipse IDE。

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.

Servlet容器是Servlet API的具体实现。还要注意,例如WildFly和GlassFish不仅仅是一个servlet容器,它们还支持JSF (Faces)、EJB (Enterprise Beans)、JPA (Persistence)和所有其他Jakarta EE的功能。请参见a.o。Java EE到底是什么?

确保使用了正确的Servlet包

javax。*包已重命名为jakarta。*从Servlet API 5.0版本开始包,它是Jakarta EE 9的一部分(Tomcat 10, TomEE 9, WildFly 22预览,GlassFish 6, Payara 6, Liberty 22等)。因此,如果您的目标是这些服务器版本或更新版本,则需要进行替换

import javax.servlet.*;
import javax.servlet.http.*;

by

import jakarta.servlet.*;
import jakarta.servlet.http.*;

为了让它编译,否则您可能会面临这个构建错误的风险

超类“javax.servlet.http”。在Java构建路径中找不到HttpServlet

在Eclipse中集成服务器并将其与项目相关联

在您的机器上安装了Eclipse for Enterprise Java和servlet容器之后,在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.

永远不要携带松散的特定于服务器的JAR文件

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.

在使用Maven的情况下,需要绝对确保已经由目标运行时提供的特定于servletcontainer的库被标记为<scope>provided</scope>。你可以在这个回答中找到Tomcat 10+、Tomcat 9-、JEE 9+和JEE 8-的正确pom.xml依赖声明的例子:如何在Maven pom.xml中为Tomcat正确配置Jakarta EE库?

以下是一些典型的异常,当你在/WEB-INF/lib甚至/JRE/lib, /JRE/lib/ext等特定于servletcontainer的库中粗心地试图修复编译错误时,你会得到:

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