簡體   English   中英

HTTP 狀態 405 - 此 URL 不支持 HTTP 方法 POST

[英]HTTP status 405 - HTTP method POST is not supported by this URL

當我運行我的項目時,出現“此 URL 不支持 HTTP 方法 POST”錯誤。 有趣的是,兩天前它運行得非常好。 在我對我的代碼進行了一些更改但隨后恢復了我的原始代碼並且它給了我這個錯誤之后。 請你幫助我好嗎?

這是我的 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 v3 服務器 - 讓我知道您需要知道的任何其他信息

這是因為在您的doGet()doPost()方法上,您將其稱為super方法。 而是在上述各個方法中調用processRequest()

默認情況下, super.doGet()super.doPost()方法返回HTTP 405,因此您不必調用超類doGet()doPost()

為什么您的代碼中有一個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);

doPost()內部,您必須調用processRequest()

您在調用doGet()doPost()方法時並未實際實現(使用super )。

HttpServlet基本上遵循模板方法模式,其中所有未覆蓋的HTTP方法都返回HTTP 405錯誤“ Method not support”。 覆蓋此類方法時,不應調用super方法,因為否則您仍然會收到HTTP 405錯誤。 您的doPost()方法也是如此。

在上述各個方法中調用processRequest(req,resp)

編輯:

第二,

不要使用調度程序將請求轉發到HTML。 如果只想顯示html,請使用重定向。

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

另外,優良作法是在注銷或發送無效憑據時使用redirect

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM