【发布时间】: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