我想在Eclipse中使用servlet进行开发,但它说包javax。Servlet / jakarta。Servlet无法解析。如何添加javax。Servlet / jakarta。servlet包到我的Eclipse项目?
当前回答
在我的例子中,当我转到Targetted Runtimes,屏幕时,Tomcat 7没有被列出(禁用),尽管已经安装。
为了解决这个问题,我不得不去首选项->服务器->运行时环境,然后卸载并重新安装Tomcat 7。
其他回答
我在与“动态Web模块”相关的项目创建过程中得到了一个空指针异常。
让项目进行编译(即,到javax。我必须去项目的属性,在侧边栏中选择项目facet,勾选动态Web模块,然后单击应用。
令人惊讶的是,这一次“动态Web模块”facet安装正确,导入开始工作。
从维基百科。
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文件。
确保您拥有正确的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
假设有一个简单的动态Web项目。确保Tomcat存在于项目的构建路径中。
首先在您的机器上下载/安装本地tomcat实例。假设我们安装了tomcat v8.5。
现在配置Eclipse全局服务器运行时环境
Windows > Preferences > Server > Runtime Environments > Add > Apache > Apache Tomcat v8.5 > Next > Browse Tomcat Installation directory > Next > Finish.
现在创建动态web项目,我们看到导入javax。Servlet无法解决问题。请按照以下步骤解决此问题。
Right Click On Project > Build Path > Configure Build Path > Libraries > Add Library > Server Runtime > Select your Apache Tomcat instance > Finish > Apply and Close.
所有的错误都会消失。
在我的例子中,当我转到Targetted Runtimes,屏幕时,Tomcat 7没有被列出(禁用),尽管已经安装。
为了解决这个问题,我不得不去首选项->服务器->运行时环境,然后卸载并重新安装Tomcat 7。
推荐文章
- 如何分割逗号分隔的字符串?
- Java字符串—查看字符串是否只包含数字而不包含字母
- Mockito.any()传递带有泛型的接口
- 在IntelliJ 10.5中运行测试时,出现“NoSuchMethodError: org.hamcrest. matcher . descripbemismatch”
- 使用String.split()和多个分隔符
- Java数组有最大大小吗?
- 在Android中将字符串转换为Uri
- 从JSON生成Java类?
- 为什么java.util.Set没有get(int index)?
- Swing和AWT的区别是什么?
- 为什么Java流是一次性的?
- 四舍五入BigDecimal *总是*有两位小数点后
- 设计模式:工厂vs工厂方法vs抽象工厂
- Java:检查enum是否包含给定的字符串?
- 它的意思是:序列化类没有声明一个静态的最终serialVersionUID字段?