当前位置: 首页 > 知识库问答 >
问题:

为什么我得到这个URL不支持的错误HTTP GET方法,而我没有在代码中的任何地方使用它

屠钊
2023-03-14
public class LoginPage extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
         String contextPath = request.getContextPath();
            out.println("<form method='post' action='LoginCheck'>");
            out.println("Username: <input type='text' name='username'>");
            out.println("<br>");
            out.println("Password: <input type='password' name='password'>");
            out.println("<br>");
            out.println("<input type='submit' value='login'>");
            out.println("<br>");
        out.println("</form>");
    }
}

LoginCheck页面此页面用于检查用户的用户名和密码,检查用户名和密码是否正确后,重定向到欢迎页面,如果用户名和密码不正确,则重定向到错误页面,每个页面的代码如下:

public class LoginCheck extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(username.equals("abs") && password.equals("abs")){
            response.sendRedirect("Welcome");
        }else{
            response.sendRedirect("Error");
        }
    }
}

如果用户名和密码正确,欢迎使用。

public class Welcome extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("<h3>Welcome user</h3>");
    }
}

如果用户名或密码不正确,则出错。

public class ErrorPage extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("<h3>ERROR !!!</h3>");
        out.println("<h3>Username or Password you entered is wrong.</h3>");
    }
}

共有1个答案

阚小云
2023-03-14

您获得HTTP 405的原因是HttpServletResponse.sendRedirect指定如下:

使用指定的重定向位置URL向客户端发送临时重定向响应。

因此,如果您执行response.sendredirect(“welcome”);,则“告诉”客户端浏览器转到相对URL“welcome”。巧合的是,这个映射到您的welcome-servlet(我想是这样的)。HTTP只支持GET重定向,因此GET请求由客户端浏览器发出,但welcome-servlet只支持POST。

public class Welcome extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        out.println("<h3>Welcome user</h3>");
    }
}
public class LoginCheck extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if(username.equals("abs") && password.equals("abs")){
            RequestDispatcher rd = request.getRequestDispatcher("Welcome");
            rd.forward(request, response);
        }else{
            RequestDispatcher rd = request.getRequestDispatcher("Error");
            rd.forward(request, response);            
        }
    }
}
 类似资料: