2013-12-08 229 views
1

我正在學習如何製作Web應用程序,並剛剛實現了一個應該在網頁上顯示當前日期的簡單應用程序。但是,我這樣做時收到錯誤。我在調試模式下運行了斷點:resp.getWriter()。println(new Date());Java Web服務器應用程序

這是我的Java代碼:

package org.test.webapp; 

import java.io.IOException; 
import java.util.Date; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class TestServlet extends HttpServlet { 

    @Override 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
      throws ServletException, IOException { 
     resp.getWriter().println(new Date()); 
    } 

} 

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" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    id="WebApp_ID" version="2.5"> 

    <servlet> 
     <servlet-name>Test</servlet-name> 
     <servlet-class>org.test.webapp/TestServlet</servlet-class> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>Test</servlet-name> 
     <url-pattern>/test</url-pattern> 
    </servlet-mapping> 



    <display-name>Example01</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> 
</web-app> 

這是我收到的錯誤:

type Exception report 

message 

description The server encountered an internal error() that prevented it from fulfilling this request. 

exception 

javax.servlet.ServletException: Error allocating a servlet instance 
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) 
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) 
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) 
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 
    java.lang.Thread.run(Unknown Source) 

root cause 

java.lang.NoClassDefFoundError: IllegalName: org.test.webapp/TestServlet 
    java.lang.ClassLoader.preDefineClass(Unknown Source) 
    java.lang.ClassLoader.defineClass(Unknown Source) 
    java.security.SecureClassLoader.defineClass(Unknown Source) 
    org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2818) 
    org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1159) 
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1647) 
    org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526) 
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298) 
    org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) 
    org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588) 
    org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 
    java.lang.Thread.run(Unknown Source) 

note The full stack trace of the root cause is available in the Apache Tomcat/6.0.32 logs. 
+0

的錯誤是,當我輸入:http://localhost:8080/Example01/test – Dheeraj

+0

我認爲錯誤在servlet的定義中,在web.xml中 – HAL9000

+0

順便說一句,如果你想開始使用web應用程序,我強烈建議使用框架像Spring MVC,而不是自己處理所有的servlet細節;他們可以爲您提供URL解析,內容協商和響應構建等管道功能。 – chrylis

回答

3

您的serlvet類應該引用班級名稱。所以不是這樣的:

<servlet-class>org.test.webapp/TestServlet</servlet-class> 

這是一種非法的類名(您的服務器無法找到,因此除外),試試這個:

<servlet-class>org.test.webapp.TestServlet</servlet-class> 
相關問題