如何在gwt客户端应用程序和谷歌应用程序引擎服务器中同步cookie

时间:2017-09-17 07:21:01

标签: java google-app-engine gwt

我为web开发gwt客户端应用程序,我使用google app engine作为服务器 我用java编写服务器 我使用RequestBuilder将请求发送到服务器 我如何在服务器和客户端之间同步cookie。 每个请求客户端发送到服务器的服务器创建新会话 我想避免它 在服务器中,我在每个请求中编写这样的代码

private void addCookies(){
    String jsessionId = request.getParameter("JSESSIONID");
    if (jsessionId != null && !jsessionId.isEmpty()) {
        Cookie userCookie = new Cookie("JSESSIONID", jsessionId);
        response.addCookie(userCookie);
    } else {
        String sessionId = request.getSession().getId();
        Cookie userCookie = new Cookie("JSESSIONID", sessionId);
        response.addCookie(userCookie);
    }
}

因此,对于第一个请求,我使用sessionId创建了cookie,并在会话中将会话ID返回给客户端

在客户端我写这样的代码

public void setSessionId(String sessionId) {
    Cookies.setCookie(JSESSIONID,  sessionId);
}

我从服务器获取sessionId并添加到cookie

当我使用RequestBuilder发送请求时,看起来像这样

public void sendRequest(final BaseRequest baseReuest){
    long currentTime = getApplicationServices().getTimeManager().getCurrentDate().getTime();
    if(isSessionIdle()){
        getBaseClientFactory().restart(true);
        return;
    }
    if(baseReuest.getOptionsRequest().isShowLoader()){
        showLoader(true);
        loaderRequestsCounter++;
    }
    try {
        String url = baseUrl + baseReuest.getMethodName();
        RequestBuilder requestBuilder = new RequestBuilder(RequestBuilder.POST, url);
        requestBuilder.setHeader("Content-type", "application/x-www-form-urlencoded");
        baseReuest.addParam(JSESSIONID, getSessionId());
        requestBuilder.sendRequest(baseReuest.getParamsAsString(), new RequestCallback() {

            @Override
            public void onResponseReceived(Request request, Response response) {
                requestFinish(baseReuest);
                int status = response.getStatusCode();
                if(status == 200){
                    String json = response.getText();
                    BaseRequestResponse baseRequestResponse = baseReuest.buildResponse(json);
                    if(!baseRequestResponse.isSuccess()){
                        showFailureMessage(baseRequestResponse.getFailureResponse());
                    }
                    baseReuest.notifyResponse(baseRequestResponse);
                }
                else{
                    onErrorResponse();
                }
            }

            @Override
            public void onError(Request request, Throwable exception) {
                onErrorResponse();
            }
            private void onErrorResponse(){
                requestFinish(baseReuest);
                String message = getLabels().getLabel(IGeneralInfoLabels.THERE_IS_PROBLEM_ACCESSING_THE_SERVER);
                if(!isOnline()){
                    message = getApplicationServices().getGeneralManager().getLocalLabel().connectionNotAvailable();
                }
                FailureResponse failureResponse = new FailureResponse(message);
                showFailureMessage(failureResponse);
                baseReuest.notifyFailureResponse(failureResponse);
            }
            private void showFailureMessage(FailureResponse failureResponse){
                if(baseReuest.getOptionsRequest().isShowMessage()){
                    BaseRequestManager.this.showFailureMessage(failureResponse);
                }
            }
        });
        lastRequestSentTime = currentTime;
    } catch (RequestException e) {
        showLoader(false);
        requestFinish(baseReuest);
    }
}

例如对于第一个请求,sessionId是jk7br57mo1e5所以我将sessionId添加到客户端的cookie中 但是在每个请求中服务器都创建了新的sessionId为什么? 谢谢

0 个答案:

没有答案