【问题标题】:HTTP Status 404 and resource not found while running servlet [duplicate]运行servlet时找不到HTTP状态404和资源[重复]
【发布时间】: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


    【解决方案1】:

    您必须在 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 方法将被执行。

    【讨论】:

      猜你喜欢
      • 2013-06-02
      • 2014-05-14
      • 2014-11-20
      • 2020-04-29
      • 2016-09-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多