Java servlets发生Http 404/500错误?

时间:2015-08-25 22:11:39

标签: java jsp java-ee servlets web

我正在关注关于servlets的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>

项目设置,如果相关: enter image description here

UPDATE :我修复了testServlet与TestServlet不同的问题。现在我收到Http 500错误: enter image description here

1 个答案:

答案 0 :(得分:-1)

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

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

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