HTTP状态405 - HTTP POST方法不受此URL支持(HTTP status 405

2019-09-18 14:29发布

当我跑我的项目,我得到这个“HTTP POST方法不受此URL支持”错误。 有趣的是,它运行完全正常,前两天。 之后,我做我的代码进行一些更改,但随后恢复了我的原代码和它给我这个错误。 请你帮助我好吗?

这里是我的index.html:

<form method="post" action="login.do">
<div>
<table>
            <tr><td>Username: </td><td><input type="text" name="e_name"/>
            </td>  </tr>
            <tr><td> Password: </td><td><input type="password" name="e_pass"/>
            </td>  </tr>
            <tr><td></td><td><input type="submit" name ="e_submit" value="Submit"/>

这里是我的登录的servlet:

public class Login extends HttpServlet {

/**
 * Processes requests for both HTTP
 * <code>GET</code> and
 * <code>POST</code> methods.
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException, SQLException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        /*
         * TODO output your page here. You may use following sample code.
         */
        int status;
        String submit = request.getParameter("e_submit");
        String submit2 = request.getParameter("a_submit");
        out.println("Here1");
        String e_name = request.getParameter("e_name");
        String e_password = request.getParameter("e_pass");
        String a_name = request.getParameter("a_name");
        String a_password = request.getParameter("a_pass");
        out.println(e_name+e_password+a_name+a_password);
        Author author = new Author(a_name,a_password);  
        Editor editor = new Editor(e_name,e_password);

      // If it is an AUTHOR login:

       if(submit==null){
       status = author.login(author);
       out.println("Author Login");
       //Incorrect login details
       if(status==0) {
           out.println("Incorrect");

       RequestDispatcher view = request.getRequestDispatcher("index_F.html");
       view.forward(request, response);

       }
       //Correct login details --- AUTHOR

       else {

              out.println("Correct login details");
              HttpSession session = request.getSession();    
              session.setAttribute(a_name, "a_name");

              RequestDispatcher view = request.getRequestDispatcher("index_S.jsp"); 
              view.forward(request, response);
            }

       }

       //If it is an EDITOR login

       else if (submit2==null){

           status = editor.login(editor);

           //Incorrect login details

           if(status==0) {

       RequestDispatcher view = request.getRequestDispatcher("index_F.html");
       view.forward(request, response);
            }

           //Correct login details --- EDITOR

           else {
               out.println("correct");
               HttpSession session = request.getSession();    
       session.setAttribute(e_name, "e_name");
       session.setAttribute(e_password, "e_pass");
               RequestDispatcher view   = request.getRequestDispatcher("index_S_1.html"); 
               view.forward(request, response);

            }           }


        out.println("</body>");
        out.println("</html>");



    } finally {            
        out.close();
    }
}

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    super.doPost(req, resp);
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    super.doGet(req, resp);
}}

而我的web.xml看起来是这样的:

<?xml version="1.0" encoding="UTF-8"?>
<servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
        <param-name>config</param-name>
        <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
        <param-name>debug</param-name>
        <param-value>2</param-value>
    </init-param>
    <init-param>
        <param-name>detail</param-name>
        <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
</servlet>
<servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>controller.Login</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>Login</servlet-name>

    <url-pattern>/login.do</url-pattern>
</servlet-mapping>
<session-config>
    <session-timeout>
        30
    </session-timeout>
</session-config>
<welcome-file-list>
    <welcome-file>index.html</welcome-file>
</welcome-file-list>

我用Glassfish的第三版服务器 - 让我知道其他任何你需要知道的

Answer 1:

为什么会出现processRequest在你的代码的方法? 谁将会调用该方法?

你无法通过调用起床该方法super.doGet()super.doPost()你需要显式调用该方法。

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    processRequest(req,resp)
}

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    processRequest(req,resp)
}

编辑

做这个

response.sendRedirect("index_F.html");
return;

代替

RequestDispatcher view = request.getRequestDispatcher("index_F.html");
view.forward(request, response);


Answer 2:

这是因为在你doGet()doPost()方法,你调用它的super方法。 相反,调用processRequest()上述各方法的内部。

super.doGet()super.doPost()方法返回一个HTTP 405,默认情况下,这样你就不会打电话给你的超类doGet()doPost()



Answer 3:

里面doPost()你要调用processRequest()



Answer 4:

你打电话doGet()doPost()没有实际执行(使用方法super )。

对于HttpServlet基本上遵循所有非重写HTTP方法返回一个模板方法模式HTTP 405错误“不支持方法”。 当你重写这样的方法,你不应该调用super方法,因为否则你将仍然得到HTTP 405错误。 同样的故事继续为您doPost()方法。

调用processRequest(req,resp)上述各方法的内部。

编辑:

第二,

不要使用调度转发请求HTML。 使用,如果你想只显示HTML重定向反正。

response.sendRedirect("index_F.html");
return;

此外,它是用很好的做法redirect当你登出或发回的无效凭证。



文章来源: HTTP status 405 - HTTP method POST is not supported by this URL