-1

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

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:

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 !!!

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

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

Abhijit Kumbhar
  • 923
  • 3
  • 23
  • 49

2 Answers2

3

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

Sends a temporary redirect response to the client using the specified redirect location 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.

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

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.

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);            
        }
    }
}
Lars Gendner
  • 1,816
  • 2
  • 14
  • 24
  • After using ` RequestDispatcher rd = request.getRequestDispatcher("Welcome"); rd.forward(request, response);` That works. :) but can you explain why is that happening here ??? – Abhijit Kumbhar Sep 02 '15 at 14:31
  • 1
    @AbhijitKumbhar have you read the link I gave you or are you still defiantly ignoring it? Please stop asking to be spoonfed and do some reading. You are never going to learn to be a skilled programmer if you cannot read simple documentation. – Boris the Spider Sep 02 '15 at 14:37
  • Sorry @BoristheSpider I didn't recognized your link, But now I read that and I understand Thank you. – Abhijit Kumbhar Sep 02 '15 at 15:07
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.

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

Community
  • 1
  • 1
Harish Ved
  • 560
  • 4
  • 17