0

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)
4

2 回答 2

3
java.lang.NoClassDefFoundError: org/eclipse/wst/web/internal/deployables/IFlatDeployable

哎呀。这是一个非常严重的错误,这个类是 Eclipse 内部的。这曾经被报告为Eclipse 问题 388698,但不幸的是,它被关闭为WORKSFORME. 毕竟,这似乎是一个特定于环境/平台/配置的问题。我会考虑重新发布相同的问题以及尽可能多的有关所使用环境的详细信息(操作系统制作/版本、JRE/JDK 制作/版本、硬件规格等)。

我个人还没有尝试过 Juno(我通常会等待 SR1 在冬天左右发布,以便涵盖所有主要的初期问题),但是如果完全删除 Eclipse 安装并重新解压缩它不起作用,那么我会建议尝试退回到以前的 Eclipse 版本,即Indigo SR2。确保为您的平台下载正确的版本(32 位与 64 位)。

于 2012-09-24T20:15:46.283 回答
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 打包战争并部署它。

(此解决方案来自 URL错误 388698 - 导出BalusC 提到的 WAR 文件时出错)

于 2012-10-16T16:38:07.720 回答