Servlet 在 Eclipse Juno for Java EE 中新创建的动态 Web 项目中不起作用。这是从头开始的过程:
- 我下载了 Eclipse EE (Juno),没有更改任何默认配置
- 我下载了最新的 Tomcat (7.0.30)
- 在Preferences > Server > Runtime Environments我添加了新的 Apache Tomcat v7.0
- 我创建了名为 UniBudget 的新动态 Web 项目(上下文根
budget
:) - 我添加了新文件(里面
index.jps
有“它可以工作!”)
此时将项目添加到服务器并运行它后,我可以看到“它可以工作了!” 在屏幕上。所以,部署工作。
接下来我添加了 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()” )。但是我得到了这个错误,而不是控制台中的打印输出:
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)