[英]Http 404/500 error with Java servlets?
I am following the Murach book on servlets and I put together a really basic servlet but I keep getting a 404 error. 我正在阅读有关servlet的Murach书,并整理了一个非常基本的servlet,但始终收到404错误。 Following is the code:
以下是代码:
The servlet code: 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);
}
}
And the web.xml file: 和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>
And the main the html page(index.html) that calls the servlet: 并且主要是调用该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>
And the project setup, if relevant: 以及项目设置(如果相关):
UPDATE : I have fixed the issue with testServlet not being same as TestServlet. 更新 :我已经解决了testServlet与TestServlet不同的问题。 Now I am getting a Http 500 error:
现在我收到一个Http 500错误:
Your class did not compile because the core servlet libraries are missing from the build path. 您的类未编译,因为构建路径中缺少核心Servlet库。
For a web application it is not just necessary to have the jars in the build path, but the related lib jars should also present under 'WebContent/WEB-INF/lib' directory. 对于Web应用程序,不仅需要在构建路径中包含jar,而且相关的lib jar也应位于“ WebContent / WEB-INF / lib”目录下。
So put the jar in the lib folder present under WebContent and try running the application. 因此,将jar放在WebContent下的lib文件夹中,然后尝试运行该应用程序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.