HTTP状态500错误实例化servlet类[重复]

Posted

技术标签:

【中文标题】HTTP状态500错误实例化servlet类[重复]【英文标题】:HTTP Status 500 Error instantiating servlet class [duplicate] 【发布时间】:2015-07-05 03:48:36 【问题描述】:

我正在编写一个 Web 应用程序并将其部署到 apache tomcat Web 容器。我按照http://cse.csusb.edu/turner/java_web_programming/servlets/ 的一个简单教程

然后最终得到这个错误。我分别粘贴了sn-ps和web.xml、website.xml和我的代码文件。

结构:

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.HomeServlet</servlet-class>
   </servlet>
   <servlet-mapping>
      <servlet-name>home</servlet-name>
      <url-pattern>/home</url-pattern>
   </servlet-mapping>
</web-app>

HomeServlet.java

打包网站.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>");
                       

错误:

HTTP Status 500 - Error instantiating servlet class website.web.HomeServlet

type Exception report

message Error instantiating servlet class website.web.HomeServlet

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

exception

javax.servlet.ServletException: Error instantiating servlet class website.web.HomeServlet
    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:610)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1517)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1474)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)
root cause

java.lang.ClassNotFoundException: website.web.HomeServlet
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1305)
    org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1157)
    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:610)
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)
    org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:223)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1517)
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1474)
    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
    java.lang.Thread.run(Thread.java:745)

【问题讨论】:

在你的 web.xml 为什么你有website.web.TestServlet 作为servlet-class? 抱歉,我更新了:website.web.HomeServlet 【参考方案1】:

我认为这部分有问题:

<servlet-class>website.web.HomeServlet</servlet-class>

改为尝试:

<servlet-class>website.HomeServlet</servlet-class>

【讨论】:

成功了,谢谢 这对我没有帮助【参考方案2】:

首先,您的 web.xml 将 TestServlet 列为服务器的类名,而不是 HomeServlet。

其次,虽然您的 HomeServlet 类似乎位于 website.web 包中,但它实际上位于名为 website 的文件夹中,而不是 website/web。看到这个编译正确我会感到惊讶吗?

【讨论】:

我原来放的是HomeServlet。我更新了一切。尽管如此,我得到了同样的错误。它确实可以编译。 嗯,错误确实是 java.lang.ClassNotFoundException: website.web.HomeServlet,所以我仍然认为您将 HomeServlet 放在错误的包中。确保它以“package website.web;”开头而不是“打包网站;”...?

以上是关于HTTP状态500错误实例化servlet类[重复]的主要内容,如果未能解决你的问题,请参考以下文章

运行Servlet时报实例化类错误,再次刷新变404

javax.servlet.ServletException: 实例化Servlet类[com.wk.web.RegisterServlet]异常

HTTP 状态 404 – 未找到!!!!或有时 Http 500 [重复]

HTTP 状态 500 – 内部服务器错误(Java、Eclipse、Servlet、Tomcat)

Javaweb中出现HTTP状态 500 - 内部服务器(servlet)

Servlet 错误:HTTP 状态 405 - 此 URL 不支持 HTTP 方法 GET [重复]