2012-11-12 71 views
0

servlet在Tomcat中正常加載。現在,我可以訪問我的原型的.html頁面就好了,但是當我點擊按鈕,執行servlet,我得到以下錯誤:無法從.html頁面執行servlet

HTTP Status 500 - Error instantiating servlet class dbServlet 

type Exception report 

message Error instantiating servlet class dbServlet 

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

exception 

javax.servlet.ServletException: Error instantiating servlet class dbServlet 
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929) 
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002) 
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585) 
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
java.lang.Thread.run(Unknown Source) 
root cause 

java.lang.ClassNotFoundException: dbServlet 
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714) 
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559) 
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472) 
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) 
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929) 
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) 
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002) 
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585) 
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310) 
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
java.lang.Thread.run(Unknown Source) 

下面是我的原型的servlet代碼:

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

@SuppressWarnings("serial") 
public class databaseServlet extends HttpServlet { 
protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
response.setContentType("text.html"); 
PrintWriter out = response.getWriter(); 

out.println("<xml version = \"1.0\"?>"); 
out.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD " + 
"XHTML 1.0 Strict//EN\" \"http://www.w3.org" + 
"/TR/xhtml1/DTD/xhtml1-strict.dtd\">"); 
out.println("<html xmlns = \"http://www.w3.org/1999/xhtml\">"); 

out.println("<head>"); 
out.println("<title>MySQL Servlet</title>"); 
out.println("<style type='text/css'>"); 
out.println("body{background-color: blue}"); 
out.println("</style>"); 
out.println("</head>"); 

out.println("<body>"); 
out.println("<h1>The MySQL Servlet Works!</h1>"); 
out.println("</body>"); 

out.println("</html>"); 
out.close(); 
} 
} 

有誰知道堆棧跟蹤可能意味着什麼?

我很感激幫助。

+0

你能分享你的web.xml嗎? –

回答

4

java.lang.ClassNotFoundException: dbServlet org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1714)

從上面堆棧跟蹤:

你的servlet databaseServlet但你打電話dbServlet。看來你的輸入有誤web.xml

注意:順便說一句,java命名約定建議類名以大寫字母開頭。

+0

我今天實際上發現了這一點,而且你是完全正確的。謝謝! – PeerPressure

+0

@ user1652842:很高興幫助你。享受編碼。 – kosa

1

您在問題中沒有包含Servlet映射。

java.lang.ClassNotFoundException: dbServlet 

您有一個名爲databaseServlet Servlet和你已經宣佈在部署描述符dbServlet

你的Servlet條目應該是這樣的。

例子:

<servlet> 
    <servlet-name>DatabaseServlet</servlet-name> 
    <servlet-class>controller.DatabaseServlet</servlet-class> 
    </servlet> 

    <servlet-mapping> 
    <servlet-name>DatabaseServlet</servlet-name> 
    <url-pattern>/dbServlet</url-pattern> 
    </servlet-mapping> 

無關的問題:

嘗試使用MVC模式,你可以JSP呈現,Servlets被控制器以重定向Web應用程序的流量和MODLE的實體。