2

我在运行我的项目时收到“此 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 服务器 - 让我知道您需要知道的其他任何信息

4

4 回答 4

5

那是因为在你的doGet()anddoPost()方法上,你正在调用它的super方法。而是调用processRequest()上面提到的各个方法。

super.doGet()andsuper.doPost()方法默认返回 HTTP 405,因此您无需调用超类doGet()doPost().

于 2012-06-27T09:04:49.817 回答
2

为什么你的代码中有一个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);
于 2012-06-27T09:30:26.757 回答
0

你必须在里面doPost()打电话processRequest()

于 2012-06-27T09:00:45.707 回答
0

您在没有实际实现(使用)的情况下调用doGet()和方法。doPost()super

HttpServlet 基本上遵循模板方法模式,其中所有未覆盖的 HTTP 方法都返回HTTP 405错误“方法不支持”。当你重写这样的方法时,你不应该调用super方法,否则你仍然会得到HTTP 405错误。您的 doPost() 方法也会发生同样的情况。

调用processRequest(req,resp)上面提到的各个方法里面。

编辑:

第二,

不要使用调度程序将请求转发到 HTML。如果您只想显示 html,请使用重定向。

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

redirect此外,在您注销或发回无效凭据时使用它是一种很好的做法。

于 2012-06-27T09:13:25.420 回答