如何在Eclipse项目中导入javax.servlet/jakarta.servlet API? [英] How do I import the javax.servlet / jakarta.servlet API in my Eclipse project?

查看:563
本文介绍了如何在Eclipse项目中导入javax.servlet/jakarta.servlet API?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Eclipse中使用Servlet进行开发,但是它说包 javax.servlet / jakarta.servlet 不能解决.如何将 javax.servlet / jakarta.servlet 包添加到我的Eclipse项目中?

解决方案

确保您具有正确的Eclipse和服务器版本

确保至少使用

b.或者通过Eclipse首选项

  • 打开窗口>偏好设置>服务器>运行时环境.

  • 您可以在此处添加编辑删除服务器.

  • 将服务器与项目关联

    a.在新项目中

    • 打开左侧的 Project Navigator/Explorer .

    • 右键单击此处,然后选择 New>项目,然后在菜单 Web>动态Web项目.

    • 在向导中,将 Target Runtime 设置为集成服务器.

    b.或者,在现有项目中

    • 右键单击项目,然后选择属性.

    • 目标运行时部分中,选择集成服务器.

    无论哪种方式,Eclipse都会自动在构建路径中使用servlet容器的库.这样,您就可以导入和使用Servlet API.

  • 从不随身携带服务器特定的松散JAR文件

    无论如何,您都无需在项目的 Build Path 属性中随意摆弄.首先,您应该从不手动复制/下载/移动/包括各个servlet容器特定的库,例如 servlet-api.jar jsp-api.jar el-api.jar j2ee.jar javaee.jar 等.这只会导致将来的可移植性,兼容性,类路径和可维护性方面的问题,因为将Web应用程序部署到与最初从那些库/版本获取库的版本/版本不同的Servlet容器中时,它将无法正常工作.

    如果您使用的是Maven,则需要绝对确保目标运行时已经提供的servlet容器特定的库被标记为< scope> provided</scope> .您可以在以下答案中找到适用于Tomcat 10 +,Tomcat 9-,JEE 9+和JEE 8的正确 pom.xml 依赖项声明的示例: java.lang.NullPointerException,位于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:未定义类型的方法getJspApplicationContext(ServletContext)JspFactory
  • java.lang.VerifyError:(类:org/apache/jasper/runtime/JspApplicationContextImpl,方法:createELResolver签名:()Ljavax/el/ELResolver;)函数的参数不兼容
  • 未加载jar.参见Servlet Spec 2.3,第9.7.2节.令人反感的类:javax/servlet/Servlet.class
  • I want to develop with Servlets in Eclipse, but it says that the package javax.servlet / jakarta.servlet cannot be resolved. How can I add javax.servlet / jakarta.servlet package to my Eclipse project?

    解决方案

    Ensure you've the right Eclipse and Server version

    Ensure that you're using at least Eclipse IDE for Enterprise Java developers (with the Enterprise). It contains development tools to create dynamic web projects and easily integrate servletcontainers (those tools are part of Web Tools Platform, WTP). In case you already had Eclipse IDE for Java (without Enterprise), and manually installed some related plugins, then chances are that it wasn't done properly. You'd best trash it and grab the real Eclipse IDE for Enterprise Java one.

    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, Oracle GlassFish, JBoss AS/WildFly, 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.

    A servletcontainer is a concrete implementation of the Servlet API. Note that the Java EE SDK download at Oracle.com basically contains GlassFish. So if you happen to already have downloaded Java EE SDK, then you basically already have GlassFish. Also note that for example GlassFish and JBoss AS/WildFly are more than just a servletcontainer, they also supports JSF, EJB, JPA and all other Java EE fanciness. See also a.o. What exactly is Java EE?

    Ensure that you're using the right Servlet package

    The javax.* package has been renamed to jakarta.* package since Servlet API version 5.0 which is part of Jakarta EE 9 (Tomcat 10, TomEE 9, WildFly 22 Preview, GlassFish 6, Payara 6, Liberty 22, etc). So if you're targeting these server versions or newer, then you need to replace

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

    by

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

    in order to get it to compile, else you might risk to face this build error

    The superclass "javax.servlet.http.HttpServlet" was not found on the Java Build Path

    Integrate Server in Eclipse and associate it with Project

    Once having installed both Eclipse for Enterprise Java and a servletcontainer on your machine, do the following steps in Eclipse:

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

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

    Never carry around loose server-specific JAR files

    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.

    In case you're using Maven, you need to make absolutely sure that servletcontainer-specific libraries which are already provided by the target runtime are marked as <scope>provided</scope>. You can find examples of proper pom.xml dependency declarations for Tomcat 10+, Tomcat 9-, JEE 9+ and JEE 8- in this answer: Tomcat 9 casting servlets to javax.servlet.Servlet instead of jakarta.servlet.http.HttpServlet

    Here are some typical exceptions which you can get when you litter the /WEB-INF/lib or even /JRE/lib, /JRE/lib/ext, etc with servletcontainer-specific libraries in a careless attempt to fix the compilation errors:

    这篇关于如何在Eclipse项目中导入javax.servlet/jakarta.servlet API?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆