简体   繁体   English

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

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

I've tried all methods available on StackOverflow and the internet, but nothing seems to workout as intended, below's a list of what I've tried doing:我已经尝试了 StackOverflow 和 Internet 上可用的所有方法,但似乎没有按预期进行锻炼,以下是我尝试过的操作列表:

  1. All declared methods in my java files are protected named doPost()我的 java 文件中所有声明的方法都protected ,命名为doPost()
  2. I tried using sendRedirect() method instead of RequestDispatcher()我尝试使用sendRedirect()方法而不是RequestDispatcher()
  3. I also tried using another method and calling it in doPost()我也尝试使用另一种方法并在doPost()中调用它
  4. I also tried creating another web project in Eclipse, but that too didn't work我还尝试在 Eclipse 中创建另一个 web 项目,但这也没有用

Any help would be appreciated, thanks!任何帮助将不胜感激,谢谢!

Here is my login.html这是我的登录名。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验证.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 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主页.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 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>

Why you have this problem in the first place:为什么你首先会遇到这个问题:

This chunk of code is MUCH TOO LONG to try all in one step.这段代码太长了,无法一步完成。 Especially if you don't have experience working with servlets.特别是如果您没有使用 servlets 的经验。 If you are not yet sure if your servlets and basic mappings work, start small - a single servlet, a single GET mapping - that you can try in the browser, without any HTML.如果您还不确定您的 servlets 和基本映射是否有效,请从小处着手 - 单个 servlet、单个 GET 映射 - 您可以在浏览器中尝试,无需任何 HTML。 Just two files in the entire project: web.xml and MyServlet.java - and just leave the doGet method empty.整个项目中只有两个文件: web.xmlMyServlet.java - 并将doGet方法留空。

Right now, your code is so long, that you simply cannot guess the problem once you hit it - there's too many places to verify!现在,你的代码太长了,一旦你碰到它,你根本猜不到问题——有太多地方需要验证!

The magic formula is: "try just one thing at a time".神奇的公式是:“一次只尝试一件事”。

Solution :解决方案

Remove the doGet(request, response) call in your doPost method.删除 doPost 方法中的doGet(request, response)调用。

You don not provide your own doGet method, so it defaults to what's in the superclass.您不提供自己的doGet方法,因此它默认为超类中的内容。 It so happens, that the default doGet method throws an exception stating that the GET method is not supported.碰巧的是,默认的doGet方法会引发异常,指出不支持 GET 方法。 Since you call it from your doPost , the propagating exception makes container think (rightly so) that it's your doPost that is throwing, and so that it is POST that is unsupported.由于您从doPost调用它,因此传播的异常使容器认为(正确地)它是您的doPost正在抛出,因此它是不受支持的 POST 。

Also, the call makes no sense.而且,这个电话毫无意义。

BTW: Notice that you would find this error in 5 seconds, if you first tried with the suggested two class approach: you would only have this one thing to verify.顺便说一句:请注意,如果您首先尝试使用建议的两种 class 方法,您会在 5 秒内发现此错误:您只需验证这一点。

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

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