无法从.html页面执行servlet

时间:2012-11-12 04:53:01

标签: java tomcat servlets

servlet在Tomcat中加载正常。现在我可以正常访问我的原型.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();
}
}

有谁知道堆栈跟踪可能意味着什么?

我很感激帮助。

2 个答案:

答案 0 :(得分:4)

  

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

从上面的堆栈跟踪:

您的servlet是databaseServlet,但您正在呼叫dbServlet。您似乎在web.xml

中输入错误

注意:顺便说一下,java命名约定建议类名以大写字母开头。

答案 1 :(得分: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进行演示,将Servlet作为控制器重定向Web应用程序的流程,将Modle重定向为实体。