1

我正在尝试一个基本的 servlet 应用程序。我创建了一个动态 Web 项目并将 HelloWorld.java 添加到 src 文件夹并编辑 web.xml,如下所示。然后我在服务器上运行该项目,但出现如下错误。我在线检查了解决方案,但它没有解决我的问题..有什么帮助吗?

HelloWorld.java

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;

     public class HelloWorld extends HttpServlet{ 
      public void doGet(HttpServletRequest request, 
      HttpServletResponse response)
      throws ServletException,IOException{
      response.setContentType("text/html");
      PrintWriter pw = response.getWriter();
      pw.println("<html>");
      pw.println("<head><title>Hello World</title></title>");
      pw.println("<body>");
      pw.println("<h1>Hello World</h1>");
      pw.println("</body></html>");
      }
    }

Web.xml

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!--<!DOCTYPE web-app
     PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
     "http://java.sun.com/dtd/web-app_2_3.dtd"> -->

    <web-app>
     <servlet>
      <servlet-name>Hello</servlet-name>
      <servlet-class>HelloWorld</servlet-class>
     </servlet>
     <servlet-mapping>
     <servlet-name>Hello</servlet-name>
     <url-pattern>/HelloWorld</url-pattern>
     </servlet-mapping>
    </web-app>

错误是,

Sep 23, 2013 2:38:37 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:testasu' did not find a matching property.
Sep 23, 2013 2:38:37 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:HelloWorld' did not find a matching property.
Sep 23, 2013 2:38:37 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: /opt/jdk1.6.0_21/jre/lib/i386/server:/opt/jdk1.6.0_21/jre/lib/i386:/opt/jdk1.6.0_21/jre/../lib/i386:/usr/lib/jvm/java-6-openjdk/jre/lib/i386/client:/usr/lib/jvm/java-6-openjdk/jre/lib/i386:/usr/lib/xulrunner-addons:/usr/lib/xulrunner-addons:/usr/java/packages/lib/i386:/lib:/usr/lib
Sep 23, 2013 2:38:37 PM org.apache.coyote.http11.Http11Protocol init
INFO: Initializing Coyote HTTP/1.1 on http-8080
Sep 23, 2013 2:38:37 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 694 ms
Sep 23, 2013 2:38:38 PM org.apache.catalina.core.StandardService start
INFO: Starting service Catalina
Sep 23, 2013 2:38:38 PM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: Apache Tomcat/6.0.18
Sep 23, 2013 2:38:38 PM org.apache.coyote.http11.Http11Protocol start
INFO: Starting Coyote HTTP/1.1 on http-8080
Sep 23, 2013 2:38:38 PM org.apache.jk.common.ChannelSocket init
INFO: JK: ajp13 listening on /0.0.0.0:8009
Sep 23, 2013 2:38:38 PM org.apache.jk.server.JkMain start
INFO: Jk running ID=0 time=0/24  config=null
Sep 23, 2013 2:38:38 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 586 ms
Sep 23, 2013 2:40:01 PM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet Hello as unavailable
Sep 23, 2013 2:40:01 PM org.apache.catalina.core.ApplicationContext log
SEVERE: Error loading WebappClassLoader
  delegate: false
  repositories:
    /WEB-INF/classes/
----------> Parent Classloader:
org.apache.catalina.loader.StandardClassLoader@176c74b
 HelloWorld
java.lang.ClassNotFoundException: HelloWorld
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1094)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:808)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)
Sep 23, 2013 2:40:01 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet Hello
java.lang.ClassNotFoundException: HelloWorld
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1387)
    at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1233)
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1094)
    at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:808)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:286)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:845)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:583)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:619)

这些类没有在 WEB-INF 下生成我搜索了很多关于这个问题但没有令人满意的答案来解决我的问题。我正在尝试使用servelets、jsp和html的很多应用程序,但是这个问题的bcoz似乎没有任何工作..有什么帮助吗?

4

2 回答 2

6

清理后刷新您的项目并尝试再次部署它..

于 2013-09-23T09:45:53.587 回答
2
   <servlet-class>HelloWorld</servlet-class>

那是错的。

您必须提供完全限定名称(包)名称。

<servlet-class>com.some.something.HelloWorld</servlet-class>

文档

于 2013-09-23T09:39:04.207 回答