【发布时间】:2015-04-03 03:10:54
【问题描述】:
这是我的 Servlet 类。我将 index.html 放在 project>webcontent>index.html 中。我在 web.xml 文件中使用了欢迎文件列表,但我的 servlet 仍然无法正常运行并显示 HTTP 状态 404 错误
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 MyServlet2 extends HttpServlet
{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// TODO Auto-generated method stub
super.doPost(req, resp);
resp.setContentType("text/html");
PrintWriter out = resp.getWriter();
try {
String user=req.getParameter("user");
out.println("<h2> Welcome "+user+"</h2>");
} finally {
out.close();
}
}
}
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0">
<welcome-file-list>
<welcome-file>msg.html</welcome-file>
</welcome-file-list>
</web-app>
【问题讨论】:
标签: java servlets http-status-code-404