我已经花费了几个小时的时间来解决这个问题。这是我第一次尝试编写Web应用程序并将其部署到Apache Tomcat Web容器中。我按照这里的简单教程进行操作,但最终出现了错误。我尝试了其他帖子中提出的一些解决方案,但似乎都没有起作用。下面分别粘贴了我的web.xml、website.xml和代码文件的片段,请检查我是否做错了什么。谢谢!
以下是我在 localhost_access_log 中看到的内容:
这是从包资源管理器中的项目树截图:
web.xml:
<?xml version="1.0"?>
<web-app
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<servlet>
<servlet-name>home</servlet-name>
<servlet-class>website.web.TestServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>home</servlet-name>
<url-pattern>/home</url-pattern>
</servlet-mapping>
</web-app>
website.xml:
<Context path="/website" docBase="C:\Users\Priyankar\workspace\website\web" />
HomeServlet.java
package website.web;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HomeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
PrintWriter writer = resp.getWriter();
writer.println("<h1>Hello, World!</h1>");
}
}
以下是我在 localhost_access_log 中看到的内容:
0:0:0:0:0:0:0:1 - admin [21/Jun/2013:13:25:40 -0500] "POST /manager/html/deploy?org.apache.catalina.filters.CSRF_NONCE=435C56002971030D163AA1C798F91722 HTTP/1.1" 200 14076
0:0:0:0:0:0:0:1 - - [21/Jun/2013:13:28:50 -0500] "GET /website/home HTTP/1.1" 405 1065
管理员日志:
Jun 21, 2013 1:25:40 PM org.apache.catalina.core.ApplicationContext log
INFO: HTMLManager: init: Associated with Deployer 'Catalina:type=Deployer,host=localhost'
Jun 21, 2013 1:25:40 PM org.apache.catalina.core.ApplicationContext log
INFO: HTMLManager: init: Global resources are available
Jun 21, 2013 1:25:40 PM org.apache.catalina.core.ApplicationContext log
INFO: HTMLManager: install: Installing context configuration at 'C:\Users\Priyankar\workspace\website\website.xml'
Jun 21, 2013 1:25:40 PM org.apache.catalina.core.ApplicationContext log
INFO: HTMLManager: list: Listing contexts for virtual host 'localhost'
除非您想查看特定的日志文件,否则其余部分对我来说似乎是正常的。
错误信息:
HTTP Status 500 - Error instantiating servlet class website.web.TestServlet
type Exception report
message Error instantiating servlet class website.web.TestServlet
description The server encountered an internal error that prevented it from fulfilling this request.
exception
javax.servlet.ServletException: Error instantiating servlet class website.web.TestServlet
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:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
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: website.web.TestServlet
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:953)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1023)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
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)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.41 logs.
Apache Tomcat/7.0.41
这是从包资源管理器中的项目树截图:
![enter image description here](https://istack.dev59.com/UkfmP.webp)
{WORKSPACE}\website\web\WEB-INF\classes\website\web
路径下的TestServlet.class文件。如果没有,请从Eclipse中执行Project > Clean
操作。 - Ravi K Thapliyal