gpt4 book ai didi

java - HelloWorld servlet 中的 Tomcat 404 classNotFoundException

转载 作者:行者123 更新时间:2023-11-28 22:19:34 26 4
gpt4 key购买 nike

我遇到了大多数类似的问题并尝试了大多数解决方案,但对我来说没有任何效果!

我是 Java EE 的新手,这是我的第一个 servlet。

这是我的 servlet,放在:C:\tomcat\webapps\jspbook\WEB-INF\classes

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<head>");
out.println("<title> hello World Title </title>");
out.println("</head>");
out.println("<body>");
out.println("<h1> Hello To World in body!</h1>");
out.println("</body>");
out.println("</html>");
}
}

这是 C:\tomcat\webapps\jspbook\WEB-INF 中的 web.xml 文件

 <servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>HelloWorld</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>


<welcome-file-list>
<welcome-file> welcome.html </welcome-file>
</welcome-file-list>

</web-app>

现在,当我重新启动 tomcat 并尝试 http://127.0.0.1:8080/jspbook/HelloWorld

第一次这是我的第一个错误:

type Exception report

message Error instantiating servlet class HelloWorld

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

exception

javax.servlet.ServletException: Error instantiating servlet class HelloWorld
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2462)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2451)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:722)

root cause

java.lang.ClassNotFoundException: HelloWorld
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1720)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1571)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2462)
org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2451)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Thread.java:722)

当我重新加载页面时,这是我的第二个错误:

HTTP Status 404 -

type Status report

message

description The requested resource is not available.

我的代码有什么问题?

更新:

我创建了一个包并将其命名为 com.jspbook 并将其放在此处 C:\tomcat\webapps\jspbook\WEB-INF\classes\com\jspbook\HelloWorld.java

package com.jspbook;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class HelloWorld extends HttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();

out.println("<html>");
out.println("<head>");
out.println("<title> hello World Title </title>");
out.println("</head>");
out.println("<body>");
out.println("<h1> Hello To World in body! </h1>");
out.println("</body>");
out.println("</html>");
}
}

web.xml 在这里:C:\tomcat\webapps\jspbook\WEB-INF\web.xml

<web-app xmlns="http://java.sun.com/xml/ns/j2ee" version="2.4">

<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.jspbook.HelloWorld</servlet-class>
</servlet>

<servlet-mapping>

<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>

</servlet-mapping>



<welcome-file-list>

<welcome-file> welcome.html </welcome-file>

</welcome-file-list>

</web-app>

我尝试 http://127.0.0.1:8080/jspbook/HelloWorld 但同样的错误出现了!

(根本就没有编译好的class文件)

最佳答案

请将 servlet 打包。容器无法从默认包加载类。

关于java - HelloWorld servlet 中的 Tomcat 404 classNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27066311/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com