【问题标题】:HTTP Status 405: HTTP method POST is not supported by this URLHTTP 状态 405:此 URL 不支持 HTTP 方法 POST
【发布时间】:2021-05-21 04:05:57
【问题描述】:

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

  1. 我的 java 文件中所有声明的方法都是 protected 命名为 doPost()
  2. 我尝试使用sendRedirect() 方法而不是RequestDispatcher()
  3. 我还尝试使用另一种方法并在doPost() 中调用它
  4. 我也尝试在 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>

认证.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>

【问题讨论】:

    标签: java servlets


    【解决方案1】:

    为什么会出现这个问题:

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

    现在,您的代码太长了,以至于您一旦点击它就无法猜测问题 - 有太多地方需要验证!

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

    解决方案

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

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

    而且,这个调用毫无意义。

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

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 2016-01-03
      • 2015-03-11
      • 2011-07-19
      • 2014-01-16
      • 2012-12-14
      • 2011-05-16
      相关资源
      最近更新 更多