简体   繁体   中英

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:

  1. All declared methods in my java files are protected named doPost()
  2. I tried using sendRedirect() method instead of RequestDispatcher()
  3. I also tried using another method and calling it in doPost()
  4. I also tried creating another web project in Eclipse, but that too didn't work

Any help would be appreciated, thanks!

Here is my 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>

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. 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. Just two files in the entire project: web.xml and MyServlet.java - and just leave the doGet method empty.

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.

You don not provide your own doGet method, so it defaults to what's in the superclass. It so happens, that the default doGet method throws an exception stating that the GET method is not supported. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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