HTTP 状态 500 – 内部服务器错误(Java、Eclipse、Servlet、Tomcat)
Posted
技术标签:
【中文标题】HTTP 状态 500 – 内部服务器错误(Java、Eclipse、Servlet、Tomcat)【英文标题】:HTTP Status 500 – Internal Server Error (Java, Eclipse, Servlets, Tomcat) 【发布时间】:2020-02-13 19:11:51 【问题描述】:我正在关注一些 Java 的 Servlets tuts,并一遍又一遍地发现同样的问题。我可以在 Tomcat 8.5.47 中提供单个页面,但是当我尝试将 Servlet 绑定到表单操作时,例如,当它执行时出现以下错误: Message Error instantiating servlet class [xxx.xxx.SearchServlet]
这是一个“动态网络项目”。我检查了项目属性和 Source src 部分指向 Deploy Path WEB-INF/classes。此外,所有目录都包含在构建路径中。
这里是主要文件:
src/xxx.xxx/SearchServlet.java
package xxx.xxx;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SearchServlet extends HttpServlet
private static final long serialVersionUID = 1L;
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException
String str = req.getParameter("str_search");
PrintWriter out = res.getWriter();
out.println(str);
WebContent/WEB-INF/web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
<servlet>
<servlet-name>search</servlet-name>
<servlet-class>SearchServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>search</servlet-name>
<url-pattern>/search</url-pattern>
</servlet-mapping>
</web-app>
这是完整的报告:
HTTP 状态 500 – 内部服务器错误 类型异常报告 实例化 servlet 类 [SearchServlet] 的消息错误 说明 服务器遇到了阻止它完成请求的意外情况。 例外 javax.servlet.ServletException:实例化 servlet 类 [SearchServlet] 时出错 org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:528) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:748) 根本原因 java.lang.ClassNotFoundException: SearchServlet org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1360) org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1182) org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:528) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81) org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343) org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:798) org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66) org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:810) org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500) org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49) java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) java.lang.Thread.run(Thread.java:748) 注意服务器日志中提供了根本原因的完整堆栈跟踪。 Apache Tomcat/8.5.47我想知道这是否与 Eclipse 安装权限有关,或者可能与项目结构有关?任何人有任何提示?也许我应该寻找一个容器图像或其他东西来尝试构建它?谢谢。
【问题讨论】:
【参考方案1】:只需在<servlet-class>
标记中添加类的完整限定名即可。
例子:
<servlet-class>com.stack.SearchServlet</servlet-class>
或者,如果您愿意:
package com.stack;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class MyServlet
*/
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public MyServlet()
super();
// TODO Auto-generated constructor stub
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
// TODO Auto-generated method stub
response.getWriter().append("Served at: ").append(request.getContextPath());
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
// TODO Auto-generated method stub
doGet(request, response);
【讨论】:
消息错误实例化 servlet 类 [com.crzy.SearchServlet] 是否有替代方案? 好像没有识别路径 是的,我补充一下答案。 我收到以下带有注释的错误:HTTP 状态 404 – 未找到类型状态报告消息 /Crzy/SearchServlet 描述 源服务器未找到目标资源的当前表示或不愿意透露那个存在。 你能分享你带注释的servlet的代码吗?以上是关于HTTP 状态 500 – 内部服务器错误(Java、Eclipse、Servlet、Tomcat)的主要内容,如果未能解决你的问题,请参考以下文章