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

时间:2021-09-19 05:50:44

I am developing login servlet app my code is as follows:

我正在开发登录servlet app我的代码如下:

Login page for displaying two text box and one login button. Where user enter username and password and submit data.

用于显示两个文本框和一个登录按钮的登录页面。用户输入用户名和密码并提交数据。

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 page This page is for checking user's username and password.After checking if username and password is correct then it redirect to welcome page or if it is incorrect then it redirects to Error page And the code for each page is as follows:

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");
        }
    }
}

Welcome If username and password correct.

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

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>");
    }
}

Error If username or password is Incorrect.

错误如果用户名或密码不正确。

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>");
    }
}

OK !!! But after entering username and password to login page it goes to checklogin and when it goes towards welcome page or error page it gives error !!!

好 !!!但是在输入登录页面的用户名和密码后,它会转到checklogin,当它进入欢迎页面或错误页面时,它会给出错误!

HTTP Status 405 - HTTP method GET is not supported by this URL

HTTP状态405 - 此URL不支持HTTP方法GET

I am not using GET method anywhere in above code as you can see but why I am getting this type of error ???

我没有在上面的代码中使用GET方法,你可以看到,但为什么我得到这种类型的错误?

2 个解决方案

#1


3  

The reason why you get HTTP 405 is that HttpServletResponse.sendRedirect is specified like this:

你得到HTTP 405的原因是HttpServletResponse.sendRedirect是这样指定的:

Sends a temporary redirect response to the client using the specified redirect location URL.

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

So if you do a response.sendRedirect("Welcome");, you "tell" the client browser to go to the relative URL "Welcome" instead. This by coincidence is mapped to your Welcome-servlet (I presume). HTTP only supports GET redirects, so a GET request is issued by the client browser, but your Welcome-servlet only supports POST.

因此,如果您执行response.sendRedirect(“欢迎”);,则“告诉”客户端浏览器转到相对URL“欢迎”。这巧合映射到您的Welcome-servlet(我推测)。 HTTP仅支持GET重定向,因此客户端浏览器发出GET请求,但您的Welcome-servlet仅支持POST。

If you change your Welcome-servlert like this it should work:

如果您像这样更改Welcome-servlert,它应该工作:

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>");
    }
}

Remember to change your ErrorPage-servlet as well.

请记住也要更改ErrorPage-servlet。

BUT heed the following!

但请注意以下事项!

You should not use a client side redirect but a server side forward like this:

您不应该使用客户端重定向,而是服务器端转发,如下所示:

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);            
        }
    }
}

#2


0  

Change

protected void doPost(HttpServletRequest request, HttpServletResponse response)

to

protected void doGet(HttpServletRequest request, HttpServletResponse response)

in you Welcome and Error methods. Thanks Andrew Mairose for pointing that out in comments.

在你的欢迎和错误方法。感谢Andrew Mairose在评论中指出这一点。

Reference - http://www.wellho.net/resources/ex.php4?item=j601/demolet.java

参考 - http://www.wellho.net/resources/ex.php4?item=j601/demolet.java

#1


3  

The reason why you get HTTP 405 is that HttpServletResponse.sendRedirect is specified like this:

你得到HTTP 405的原因是HttpServletResponse.sendRedirect是这样指定的:

Sends a temporary redirect response to the client using the specified redirect location URL.

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

So if you do a response.sendRedirect("Welcome");, you "tell" the client browser to go to the relative URL "Welcome" instead. This by coincidence is mapped to your Welcome-servlet (I presume). HTTP only supports GET redirects, so a GET request is issued by the client browser, but your Welcome-servlet only supports POST.

因此,如果您执行response.sendRedirect(“欢迎”);,则“告诉”客户端浏览器转到相对URL“欢迎”。这巧合映射到您的Welcome-servlet(我推测)。 HTTP仅支持GET重定向,因此客户端浏览器发出GET请求,但您的Welcome-servlet仅支持POST。

If you change your Welcome-servlert like this it should work:

如果您像这样更改Welcome-servlert,它应该工作:

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>");
    }
}

Remember to change your ErrorPage-servlet as well.

请记住也要更改ErrorPage-servlet。

BUT heed the following!

但请注意以下事项!

You should not use a client side redirect but a server side forward like this:

您不应该使用客户端重定向,而是服务器端转发,如下所示:

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);            
        }
    }
}

#2


0  

Change

protected void doPost(HttpServletRequest request, HttpServletResponse response)

to

protected void doGet(HttpServletRequest request, HttpServletResponse response)

in you Welcome and Error methods. Thanks Andrew Mairose for pointing that out in comments.

在你的欢迎和错误方法。感谢Andrew Mairose在评论中指出这一点。

Reference - http://www.wellho.net/resources/ex.php4?item=j601/demolet.java

参考 - http://www.wellho.net/resources/ex.php4?item=j601/demolet.java