【问题标题】:Http 404/500 error with Java servlets?Java servlet 出现 Http 404/500 错误?
【发布时间】:2015-08-25 22:11:39
【问题描述】:

我正在关注 Murach 关于 servlet 的书,我编写了一个非常基本的 servlet,但我不断收到 404 错误。以下是代码:

servlet 代码:

package murach;

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 TestServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
    PrintWriter output = response.getWriter();
    response.setContentType("text/html");
    output.write("<h1>" + request.getParameter("txtInput") + "</h1>");
    output.close(); 
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
{
    doPost( request, response);
}
}

还有 web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"....>
 <display-name>Servlet</display-name>

<servlet>
    <servlet-name>TestServlet</servlet-name>
    <servlet-class>murach.TestServlet</servlet-class>
</servlet>

<servlet-mapping>
     <servlet-name>TestServlet</servlet-name>
     <url-pattern>/testServlet</url-pattern>
</servlet-mapping>

<welcome-file-list>
   <welcome-file>index.html</welcome-file>
   </welcome-file-list>
</web-app>

以及调用servlet的主要html页面(index.html):

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Test Servlet</title>
</head>
<body>
<form action="testServlet" method="post">
    <input type="text" name="txtInput">
    <input type="submit" value="Submit">
</form>
</body>
</html>

以及项目设置(如果相关):

更新:我已经解决了 testServlet 与 TestServlet 不同的问题。现在我收到一个 Http 500 错误:

【问题讨论】:

  • testServlet != TestServlet。案件很重要。另外,不要关闭作者。这就是 servlet 容器的工作。
  • 你确定你重新部署了 webapp 吗?顺便说一句,你为什么使用 web.xml 而不是注释?
  • 我正在关注 Murach 的使用 web.xml 的 JSP/Servlet 书。另外,我正在本地 tomcat 服务器上测试所有内容。我停止了服务器并重新启动它。出于某种原因,现在我收到 Http 500 错误...我将使用图像更新问题。
  • 那么它已经过时了。阅读更多最新的内容。自 2009 年 12 月起支持注释,servlet 3.0,Tomcat 7。
  • 错误 500:找不到你的类,所以它可能没有编译。停止服务器,右击选择“清理工作目录”,重新构建项目并确保在Eclipse的Errors视图中没有错误,然后重新启动服务器。

标签: java jsp jakarta-ee servlets web


【解决方案1】:

您的类未编译,因为构建路径中缺少核心 servlet 库。

对于 Web 应用程序,不仅需要在构建路径中包含 jar,而且相关的 lib jar 也应该存在于“WebContent/WEB-INF/lib”目录下。

所以将 jar 放在 WebContent 下的 lib 文件夹中,然后尝试运行应用程序。

【讨论】:

    猜你喜欢
    • 2014-01-13
    • 2015-08-04
    • 2015-03-24
    • 2012-05-15
    • 2021-08-08
    • 1970-01-01
    • 1970-01-01
    • 2019-12-05
    • 1970-01-01
    相关资源
    最近更新 更多