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


当前回答

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

希望这能帮助到一些人。

其他回答

从维基百科。

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>");
  }
}

当然,这只有在将servlet-api.jar添加到Eclipse构建路径时才有效。通常,您的应用服务器(例如Tomcat)将拥有正确的jar文件。

我在与“动态Web模块”相关的项目创建过程中得到了一个空指针异常。

让项目进行编译(即,到javax。我必须去项目的属性,在侧边栏中选择项目facet,勾选动态Web模块,然后单击应用。

令人惊讶的是,这一次“动态Web模块”facet安装正确,导入开始工作。

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

这也可能是原因。我已经提出以下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>

未解决的问题是由于排除了spring-boot-start -tomcat。只需删除<排除>…</exclusions>依赖它将解决问题,但确保这样做也将排除嵌入式tomcat服务器。

如果你也需要嵌入式tomcat服务器,你可以添加相同的依赖编译范围。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>compile</scope>
</dependency>

确保您拥有正确的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