1

我正在尝试使用 httpClient 进行登录验证并在服务器端获取无限循环。更多解释将在代码之后进行。这是我的服务器端代码

    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

从客户端我正在尝试登录,如果登录无效,服务器将返回“unknownUser”,如果登录有效,则客户端将再次发送登录请求,循环将中断。在服务器端,我得到了无限循环,服务器一次又一次地处理第一个请求参数....!!!如果有人想要我可以发布我的客户端代码以获得进一步的帮助,您可以在评论部分索取。谢谢

4

4 回答 4

1

问题出continueelse块中。它应该break也是如此。但是,更好的编写方法是没有while循环。例如

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
于 2012-10-03T11:12:25.367 回答
0
while(true){

  if(...)
   break;

这是令人讨厌的代码.. 当细节错误时你可以抛出一个异常并告诉用户他需要再次编写他的凭据。所以发送一个新的httpRequest。

于 2012-10-03T11:07:22.560 回答
0

当然你会得到一个无限循环!

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

这就是造成它的原因。

而不是continue;您应该将请求发送回请求其凭据的用户。

于 2012-10-03T11:15:35.230 回答
0

您似乎不了解 HTTP 请求的工作原理。

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.

于 2012-10-03T11:21:28.210 回答