繁体   English   中英

Java Servlet的Http 404/500错误?

[英]Http 404/500 error with Java servlets?

我正在阅读有关servlet的Murach书,并整理了一个非常基本的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错误: 在此处输入图片说明

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM