所以我试图让一个简单的Hello World应用程序使用Java servlet。这是我正在参加的Web Apps课程。我的问题是我一直收到错误,说我的“HelloCedarville.class”文件无法找到。
我尝试了许多工作来实现这一目标:
我无法弄清楚导致此错误的原因。这是完整的错误:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Wrapper cannot find servlet class com.HelloCedarville or a class it depends on
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293)
org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)
org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:776)
org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:705)
org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:898)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
java.lang.Thread.run(Thread.java:724)
root cause
java.lang.ClassNotFoundException: com.HelloCedarville
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
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:293)
org.apache.jk.server.JkCoyoteHandler.invoke(JkCoyoteHandler.java:190)
org.apache.jk.common.HandlerRequest.invoke(HandlerRequest.java:291)
org.apache.jk.common.ChannelSocket.invoke(ChannelSocket.java:776)
org.apache.jk.common.ChannelSocket.processConnection(ChannelSocket.java:705)
org.apache.jk.common.ChannelSocket$SocketConnection.runIt(ChannelSocket.java:898)
org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:690)
java.lang.Thread.run(Thread.java:724)
这是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>
<context-param>
<param-name>driver</param-name>
<param-value>com.mysql.jdbc.Driver</param-value>
</context-param>
<context-param>
<param-name>connection</param-name>
<param-value>jdbc:mysql://john.cedarville.edu/cs4220</param-value>
</context-param>
<context-param>
<param-name>username</param-name>
<param-value>cs4220</param-value>
</context-param>
<context-param>
<param-name>password</param-name>
<param-value></param-value>
</context-param>
<servlet>
<!-- Arbitrary servlet name -->
<servlet-name>servlet2</servlet-name>
<!-- The name of the class you want to run -->
<servlet-class>com.HelloCedarville</servlet-class>
</servlet>
<servlet-mapping>
<!-- Must match the same as above -->
<servlet-name>servlet2</servlet-name>
<!-- maps the servlet to a specific URL in this case it
would be judah/<your login name>/servlet/RequestInfoExample -->
<url-pattern>/HelloCedarville</url-pattern>
</servlet-mapping>
<servlet>
<!-- Arbitrary servlet name -->
<servlet-name>servlet</servlet-name>
<!-- The name of the class you want to run -->
<servlet-class>HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<!-- Must match the same as above -->
<servlet-name>servlet</servlet-name>
<!-- maps the servlet to a specific URL in this case it
would be judah/<your login name>/servlet/RequestInfoExample -->
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
以下是HelloCedarville.java的java代码:
package com;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloCedarville extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String docType = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " +
"Transitional //EN\">\n";
out.println (docType + "<html>\n" + "<head><title>" +
"Hello Cedarville</title></head>\n" + "<body>\n" +
"<h1>Hello Cedarville</h1>\n" + "</body></html>\n");
}
}
答案 0 :(得分:0)
您是否将其部署到Tomcat?在编写servlet之后,需要将其部署到容器中,以便类加载器可以找到您的类。