这是我的Servlet类。我将index.html放在项目> 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>
答案 0 :(得分:0)
您必须在web.xml
描述符中配置您的servlet:
<servlet>
<servlet-name>Servlet Name</servlet-name>
<servlet-class>package.of.MyServlet</servlet-
</servlet>
<servlet-mapping>
<servlet-name>Servlet Name</servlet-name>
<url-pattern>/myservlet</url-pattern>
</servlet-mapping>
当您在浏览器中访问http://localhost:8080/my-app-name/myservlet
时,将执行您的servlet的doGet
方法。