gpt4 book ai didi

java - HTTP 状态 500 - 在 eclipse maven 中实例化 servlet 时出错

转载 作者:行者123 更新时间:2023-11-29 04:37:57 26 4
gpt4 key购买 nike

我正在尝试使用 eclipse 在 maven 中运行一个简单的 servlet。但是收到错误 500。请看看我做错了什么。主要关心的是如何在 maven 中运行 servlet,因为我不熟悉 maven 。

索引.jsp

  <html>
<body>
<h2>Hello World!</h2>
<form action="mylog" method="post">
loginID:<input type="text" name="name"/><br/>
<input type="submit" value="login"/>

</form>
</body>
</html>

mylog.java(servlet 类)

  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 mylog extends HttpServlet {
private static final long serialVersionUID = 1L;


public mylog() {
super();
// TODO Auto-generated constructor stub
}


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
}


protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String n = request.getParameter("name");

PrintWriter out = response.getWriter();
out.print(n);
out.close();

doGet(request, response);
}

}

web.xml 和目录结构-见附图 enter image description here

错误栈

     HTTP Status 500 - Error instantiating servlet class com.issac.mylog

type Exception report

message Error instantiating servlet class com.issac.mylog

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

exception

javax.servlet.ServletException: Error instantiating servlet class com.issac.mylog
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Unknown Source)

root cause

java.lang.ClassNotFoundException: com.issac.mylog
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1333)
org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1167)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:528)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1100)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:687)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1520)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1476)
java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.lang.Thread.run(Unknown Source)

最佳答案

src/main/resources 用于放置properties文件(或静态资源),所以服务器找不到类文件,所以需要创建Java源src/main/java 文件夹下的文件。

另外,确保 .class 文件已生成并在服务器类路径中可用(即,在 WEB-INF/classes 下),这将解决上述问题java.lang.ClassNotFoundException

关于java - HTTP 状态 500 - 在 eclipse maven 中实例化 servlet 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40443192/

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