HTTP 状态 405:此 URL 不支持 HTTP 方法 POST

HTTP Status 405: HTTP method POST is not supported by this URL

我已经尝试了 Whosebug 和互联网上可用的所有方法,但似乎没有任何效果符合预期,下面列出了我尝试过的方法:

  1. 我的 java 文件中所有声明的方法都 protected 命名为 doPost()
  2. 我尝试使用 sendRedirect() 方法而不是 RequestDispatcher()
  3. 我也尝试过使用另一种方法并在 doPost()
  4. 中调用它
  5. 我也试过在 Eclipse 中创建另一个 web 项目,但这也没有用

任何帮助将不胜感激,谢谢!

这是我的login.html

<!DOCTYPE html>
<html>
    <head>
        <title>Login</title>
    </head>
    <body>
        <form action="auth" method="post">
            <table>
                <tr>
                    <td>Email ID:</td>
                    <td><input type="email" name="userEmail"></td>
                </tr>
                <tr>
                    <td>Password: </td>
                    <td><input type="password" name="userPassword"></td>
                </tr>
                <tr>
                    <td><input type="submit" value="Login"></td>
                </tr>
            </table>
        </form>
    </body>
</html>

authenticate.java

package newAuthentication;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class authenticate extends HttpServlet
{
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        String userEmail = request.getParameter("userEmail");
        String userPassword = request.getParameter("userPassword");
        if (validateUser.checkUser(userEmail, userPassword))
        {
            HttpSession session = request.getSession();
            session.setAttribute("userEmail", userEmail);
            RequestDispatcher dispatch = request.getRequestDispatcher("home");
            dispatch.forward(request, response);
        }
        else
        {
            out.println("Incorrect authentication credentials, please try again!");
            RequestDispatcher dispatch = request.getRequestDispatcher("login.html");
            dispatch.include(request, response);
        }   
    }
}

validateUser.java

package newAuthentication;
import java.sql.*;

public class validateUser
{
    public static boolean checkUser(String userEmail, String userPassword)
    {
        boolean state = false;
        try
        {
            Class.forName("com.mysql.cj.jdbc.Driver");
            Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/wad","root","root");
            PreparedStatement fetchData = connect.prepareStatement("select * from studentData where email = ?  and password = ?");
            fetchData.setString(1, userEmail);
            fetchData.setString(2, userPassword);
            ResultSet data = fetchData.executeQuery();
            state = data.next();
        }
        catch (Exception err)
        {
            err.printStackTrace();
        }
        return state;
    }
}

home.java

package newAuthentication;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class home extends HttpServlet
{
    protected void doPost(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException
    {
        doGet(request, response);
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        
        HttpSession session = request.getSession();
        String userEmail = (String)session.getAttribute("userEmail");
        out.println("Welcome user " + userEmail);
    }
}

web.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_4_0.xsd" id="WebApp_ID" version="4.0">
<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_4_0.xsd" id="WebApp_ID" version="4.0">
<display-name>newAuth</display-name>
  
  <servlet>
    <servlet-name>Login</servlet-name>
    <servlet-class>newAuthentication.authenticate</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>Login</servlet-name>
    <url-pattern>/auth</url-pattern>
  </servlet-mapping>
  
  <servlet>
    <servlet-name>Home</servlet-name>
    <servlet-class>newAuthentication.home</servlet-class>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>Home</servlet-name>
    <url-pattern>/home</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

为什么你首先遇到这个问题:

这段代码太长了,无法一步完成。特别是如果您没有使用 servlet 的经验。如果您还不确定您的 servlet 和基本映射是否有效,请从小处着手 - 一个 servlet,一个 GET 映射 - 您可以在浏览器中尝试,没有任何 HTML。整个项目中只有两个文件:web.xmlMyServlet.java - 将 doGet 方法留空。

现在,你的代码太长了,你根本猜不到问题所在——要验证的地方太多了!

神奇的公式是:“一次只尝试一件事”。

解决方法:

删除 doPost 方法中的 doGet(request, response) 调用。

您没有提供自己的 doGet 方法,因此它默认为 superclass 中的方法。碰巧,默认的 doGet 方法抛出一个异常,说明不支持 GET 方法。由于您从 doPost 调用它,因此传播的异常使容器认为(正确地)是您的 doPost 正在抛出,因此 POST 不受支持。

此外,调用没有意义。

顺便说一句:请注意,如果您首先尝试使用建议的两种 class 方法,您会在 5 秒内发现此错误:您将只有一件事情需要验证。