繁体   English   中英

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

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

我已经尝试了 StackOverflow 和 Internet 上可用的所有方法,但似乎没有按预期进行锻炼,以下是我尝试过的操作列表:

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

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

这是我的登录名。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>

验证.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;
    }
}

主页.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>

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

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

现在,你的代码太长了,一旦你碰到它,你根本猜不到问题——有太多地方需要验证!

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

解决方案

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

您不提供自己的doGet方法,因此它默认为超类中的内容。 碰巧的是,默认的doGet方法会引发异常,指出不支持 GET 方法。 由于您从doPost调用它,因此传播的异常使容器认为(正确地)它是您的doPost正在抛出,因此它是不受支持的 POST 。

而且,这个电话毫无意义。

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

暂无
暂无

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

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