Servlet无法在Eclipse Juno for Java EE中新创建的Dynamic Web Project中工作。以下是从头开始的方式:
budget
)index.jps
与“它有效!”在里面)在将项目添加到服务器并运行它之后,我可以在屏幕上看到“it works!”。因此,部署可行。
接下来我添加了servlet文件(control.Controller.java
):
package control;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet({ "/Controller", "*.do" })
public class Controller extends HttpServlet {
private static final long serialVersionUID = 1L;
public Controller() {
super();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("doGet()");
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
System.out.println("doPost()");
}
}
我将index.jsp
更改为这样,仅此而已:
<jsp:forward page="/departments.do"/>
当然还有web.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>UniBudget</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<!-- servlet definition -->
<servlet>
<servlet-name>Do</servlet-name>
<servlet-class>control.Controller</servlet-class>
</servlet>
<!-- servlet mapping -->
<servlet-mapping>
<servlet-name>Do</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Do</servlet-name>
<url-pattern>/Controller</url-pattern>
</servlet-mapping>
</web-app>
生成的.class
个文件必须在WEB-INF/classes
对吗?这就是我读到的。所以:Configure Build Path > Source tab > Browse >
我在classes
创建了WEB-INF
文件夹(Advanced
我链接到文件系统中的文件夹:... /UniBudget/build/classes
)
这是项目中的文件层次结构:
我清理并构建了UniBudget项目并重新发布到服务器。清理服务器。
我以为我现在会工作(我的意思是在控制台中显示“doGet()”或“doPost()”。但是在Console中我没有打印输出,而是出现了这个错误:
HTTP Status 404 - /budget/departments.do
--------------------------------------------------------------------------------
type: Status report
message: /budget/departments.do
description: The requested resource is not available.
当我尝试直接访问http://localhost:8080/budget/Controller
时(同时没有从index.jsp
重定向)
看起来Eclipse根本看不到servlet!你能指出我犯错的地方吗?我现在无法解决这个问题。
修改
删除多余注释(@WebServlet
)后,会发生另一个错误:
HTTP Status 503 - Servlet Do is currently unavailable
--------------------------------------------------------------------------------
type Status report
message Servlet Do is currently unavailable
description The requested service is not currently available.
控制台出错:
wrz 24, 2012 9:34:15 PM org.apache.catalina.core.ApplicationDispatcher invoke
WARNING: Servlet Do is currently unavailable
不多。但就是这样。
修改
我发现此处发生了另一个错误。我甚至不确定如何重新创建它,但至少这一个提供了更多的日志信息:
HTTP Status 500 - An exception occurred processing JSP page /index.jsp at line 3
--------------------------------------------------------------------------------
type: Exception report
message: An exception occurred processing JSP page /index.jsp at line 3
description: The server encountered an internal error that prevented it from fulfilling this request.
exception:
org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 3
1: <%@ page language="java" contentType="text/html; charset=UTF-8"
2: pageEncoding="UTF-8"%>
3: <jsp:forward page="/departments.do"/>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause:
javax.servlet.ServletException: Error instantiating servlet class control.Controller
org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:746)
org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:716)
org.apache.jsp.index_jsp._jspService(index_jsp.java:65)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause:
java.lang.ClassNotFoundException: control.Controller
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
org.apache.jasper.runtime.PageContextImpl.doForward(PageContextImpl.java:746)
org.apache.jasper.runtime.PageContextImpl.forward(PageContextImpl.java:716)
org.apache.jsp.index_jsp._jspService(index_jsp.java:65)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
修改
project right click > Export > WAR file
期间的错误:
java.lang.NoClassDefFoundError: org/eclipse/wst/web/internal/deployables/IFlatDeployable
at org.eclipse.jst.j2ee.internal.archive.operations.JavaEEComponentExportOperation.getModuleParticipants(JavaEEComponentExportOperation.java:77)
at org.eclipse.jst.j2ee.internal.archive.operations.JavaEEComponentExportOperation.getParticipants(JavaEEComponentExportOperation.java:43)
at org.eclipse.jst.j2ee.internal.archive.operations.JavaEEComponentExportOperation.createFlatComponentArchiver(JavaEEComponentExportOperation.java:91)
at org.eclipse.jst.j2ee.internal.archive.operations.ComponentExportOperation.export(ComponentExportOperation.java:191)
at org.eclipse.jst.j2ee.internal.archive.operations.ComponentExportOperation.execute(ComponentExportOperation.java:136)
at org.eclipse.wst.common.frameworks.internal.datamodel.DataModelPausibleOperationImpl$1.run(DataModelPausibleOperationImpl.java:385)
at org.eclipse.core.internal.resources.Workspace.run(Workspace.java:2344)
at org.eclipse.wst.common.frameworks.internal.datamodel.DataModelPausibleOperationImpl.runOperation(DataModelPausibleOperationImpl.java:410)
at org.eclipse.wst.common.frameworks.internal.datamodel.DataModelPausibleOperationImpl.runOperation(DataModelPausibleOperationImpl.java:360)
at org.eclipse.wst.common.frameworks.internal.datamodel.DataModelPausibleOperationImpl.doExecute(DataModelPausibleOperationImpl.java:247)
at org.eclipse.wst.common.frameworks.internal.datamodel.DataModelPausibleOperationImpl.executeImpl(DataModelPausibleOperationImpl.java:219)
at org.eclipse.wst.common.frameworks.internal.datamodel.DataModelPausibleOperationImpl.cacheThreadAndContinue(DataModelPausibleOperationImpl.java:89)
at org.eclipse.wst.common.frameworks.internal.datamodel.DataModelPausibleOperationImpl.execute(DataModelPausibleOperationImpl.java:207)
at org.eclipse.wst.common.frameworks.internal.datamodel.ui.DataModelWizard$1CatchThrowableRunnableWithProgress.run(DataModelWizard.java:220)
at org.eclipse.jface.operation.ModalContext$ModalContextThread.run(ModalContext.java:121)
答案 0 :(得分:3)
java.lang.NoClassDefFoundError: org/eclipse/wst/web/internal/deployables/IFlatDeployable
糟糕。这是一个非常严重的错误,这个类就是Eclipse的内部。这已被报告为Eclipse issue 388698,但不幸的是,这被关闭为WORKSFORME
。毕竟这似乎是一个环境/平台/配置特定的问题。我会考虑重新发布相同的问题以及有关所用环境的详细信息(OS make / version,JRE / JDK make / version,hardware specs等)。
我个人还没有尝试过Juno(我通常会等待一个SR1在冬天发布,以便涵盖所有主要的出牙问题),但如果完全删除Eclipse安装并重新解压缩它不起作用,那么我建议尝试回到之前的Eclipse版本Indigo SR2。确保为您的平台下载了正确的版本(32位对64位)。
答案 1 :(得分:0)
对我来说
java.lang.NoClassDefFoundError: org/eclipse/wst/web/internal/deployables/IFlatDeployable
错误是由以下插件引起的:
WTP Patches for org.eclipse.jst.web_core.feature 3.4.0.v201207092049-20F8O7w312318212711
在插件被禁用后,我能够使用Eclipse打包并部署它。
(此解决方案来自BalusC提及的网址Bug 388698 - Error exporting WAR file)