简体   繁体   English

为什么会出现错误“此URL不支持HTTP方法发布”?

[英]Why I got the error “HTTP method post is not supported by this URL”?

I am developing a Login app using servlets in eclipse. 我正在使用Eclipse中的servlet开发一个Login应用程序。 I created one login form which contains two text fields(for entering username and password) and one login button. 我创建了一个登录表单,其中包含两个文本字段(用于输入用户名和密码)和一个登录按钮。 When user click this button it redirects to LoginCheck page where it login details are checked. 当用户单击此按钮时,它将重定向到LoginCheck页,在此检查登录详细信息。 But problem is that when I run this project I got error : HTTP Status 405 - HTTP method POST is not supported by this URL. 但是问题是,当我运行该项目时,出现错误:HTTP状态405-此URL不支持HTTP方法POST。 What is the problem I did''t understand. 我不明白什么问题。

Below is my code for LoginPage : 下面是我的LoginPage代码:

public class LoginPage extends HttpServlet {
   protected void doGet(HttpServletRequest request, HttpServletResponse   response) throws ServletException, IOException {
   response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<form method='post' actin='LoginCheck'>");
            out.println("Username: <input type='text' name='username'>");
            out.println("Password: <input type='password' name='password'>");
            out.println("<input type='submit' value='login'>");
        out.println("</form>");
    }
}

Now when user click Login button it have to go on the LoginCheck page code for this page is as follows: 现在,当用户单击“登录”按钮时,必须进入LoginCheck页面的代码如下:

public class LoginCheck extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<h3>Checking login details...</h3>");
    }
}

and here it gives problem. 这给问题。

This is the XML file: 这是XML文件:

    <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>LoginApp</display-name>
  <servlet>
    <servlet-name>LoginPage</servlet-name>
    <servlet-class>LoginPage</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginPage</servlet-name>
    <url-pattern>/LoginPage</url-pattern>
  </servlet-mapping>


  <servlet>
    <servlet-name>LoginCheck</servlet-name>
    <servlet-class>LoginCheck</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>LoginCheck</servlet-name>
    <url-pattern>/LoginCheck</url-pattern>
  </servlet-mapping>
</web-app>

It looks like you were not actually redirecting to the LoginCheck servlet. 看来您实际上并未重定向到LoginCheck servlet。 Try using request.getContextPath instead of what you had. 尝试使用request.getContextPath而不是您所拥有的。 Here is the full code: 这是完整的代码:

public class LoginPage extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse   response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String contextPath = request.getContextPath();
        out.println("<form method='post' action='/" + contextPath + "/LoginCheck'>");
        out.println("Username: <input type='text' name='username'>");
        out.println("Password: <input type='password' name='password'>");
        out.println("<input type='submit' value='login'>");
        out.println("</form>");
    }
}

If method "get" redirects to "post", your GET request would be transformed into a POST, losing all get data. 如果方法“ get”重定向到“ post”,则您的GET请求将转换为POST,丢失所有的get数据。 So my suggestion is, call doPost method from doGet method like... 所以我的建议是,从doGet方法调用doPost方法,例如...

protected void doPost(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {

        PrintWriter out = resp.getWriter();
        out.println("<html><form method='post' action='LoginCheck'>");
        out.println("Username: <input type='text' name='username'>");
        out.println("Password: <input type='password' name='password'>");
        out.println("<input type='submit' value='login'>");
        out.println("</form></html>");
}

protected void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, IOException {
      doPost(req, resp);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM