1

I am trying to make login validation in using httpClient and getting infinite loop on server side.More explanation will be after code. here is my server side code

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
    while(true){    
        String user_name = req.getParameter("username");
        String password = req.getParameter("password");
        System.out.println("User name and password is "+user_name +"   paswword is "+password);
        resp.setContentType("text/plain");
        PrintWriter writer = resp.getWriter();
        if(user_name.equalsIgnoreCase("haseeb")){
            System.out.println("valid user name");
            writer.write("welcome");
            //writer.flush();
            break;
        }
        else{
            writer.write("unknown User");
            System.out.println("unknown user name");
            writer.flush();
            continue;
        }

    }//End of while loop

}   //End of doPost Method

from client side i am trying to login, if login is invalid the server will return "unknownUser" after that client will send again request to login if login is valid the loop will break. on server side i am getting infinite loop and server is processing on first request parameters again and again and again....!!! If any one want i can post my client side code for further assistance, you can ask for it on comments section..Thankyou

1
  • One thing to remember is my client side is a java Desktop application..not a browser Oct 3, 2012 at 11:11

4 Answers 4

1

The problem is with the continue in the else block. It should have been break as well. However, a better way of writing this would be without while loop. e.g

public void doPost(HttpServletRequest req, HttpServletResponse resp)
    String user_name = req.getParameter("username");
    String password = req.getParameter("password");
    System.out.println("User name and password is "+user_name +"   paswword is "+password);
    resp.setContentType("text/plain");
    PrintWriter writer = resp.getWriter();
    if(user_name.equalsIgnoreCase("haseeb")){
        System.out.println("valid user name");
        writer.write("welcome");
        writer.flush();
    }
    else{
        writer.write("unknown User");
        System.out.println("unknown user name");
        writer.flush();
    }

}   //End of doPost Method
3
  • 1
    brother what if user try again to login validate i have to check it again how to do that in this scenario? Oct 3, 2012 at 11:16
  • 1
    when the user tries to login again, your doPost method will be called again
    – Sameer
    Oct 3, 2012 at 11:17
  • Thanks for your help that makes some sense let me try this again without loop and comment back if i get it right this way ...so that in future any one can use it.. Oct 3, 2012 at 11:22
0

You don't seem to understand how HTTP requests work.

The user's browser sends a request to the server asking for the login page, the server responds with the HTML and that response ends there. Then, the user validates his credentials, causing the browser to send a new request, and the server responds with either the page after the login, or the login page again and a message indicating that the credentials did not match, but in both cases the response ends there again. HTTP connections are not kept, it's just a series of requests and responses in separate connections.

So you don't use a loop, you just send the appropriate HTML back, once per request.


This still holds even if the user's application is a desktop application and not a browser, as long as it uses HTTP as its transport. You can't resubmit data using the same request, at least not if you use the HttpServletRequest / HttpServletResponse paradigm.

1
  • Ok brother thanks a lot for your quick guide line... i got my self screwed, other wise i got these concepts... Oct 3, 2012 at 11:29
0
while(true){

  if(...)
   break;

this is nasty code.. you can just throw an exception when the details are wrong and tell the users he needs to write his credentials again. And so send a new httpRequest.

2
  • Brother if user is writing invalid username/password again and again and again..i have to check it inside loop. Oct 3, 2012 at 11:10
  • you don't have to write this with a while(true, you can put your check in your while statement... however this is still a bad idea. Oct 3, 2012 at 11:11
0

of course you get an infinite loop!

while(true){ 
.....
    else{
        ......
        continue;
    }
}

That is what is causing it.

Instead of continue; you should sent the request back to the user that asks for his credentials.

9
  • my client side is a desktop application..if i ask him again to enter credentials i have to check it again.... how will i do that again if not in loop? Oct 3, 2012 at 11:19
  • what do you mean desktop application? Swing based?
    – MaVRoSCy
    Oct 3, 2012 at 11:21
  • And does Servlets have to do with a swing applcation?
    – MaVRoSCy
    Oct 3, 2012 at 11:25
  • How are they integrated? How do the talk between them?
    – MaVRoSCy
    Oct 3, 2012 at 11:26
  • yes servlet will response and on the bases of these responses i will do some other actions..that's the way requirements are Oct 3, 2012 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.