The problem is that since these calls are asynchronous, the second and
third are being called before the first comes back and gives the
browser the session id. You could see the results you are expecting by
simply putting a Window.alert("Wait for it"); between the first and
second call. The amount of time it takes you to dismiss the alert
would give the server enough time to return so that the next two calls
get the same session id. Of course, that isn't the solution to your
problem. What you could do is to make a call to establish the session
as soon as possible. Then, do your general application setup. From
then on, you should be getting your session ids like you expect. Or
you could generate your own session ids and tie them to, say, the
username or something and not worry about the auto-generated session
id.HTH, Chad On Nov 3, 5:05 pm, XqSun <[email protected]> wrote: > Hi all, > > I write a simple app to get session id by calling getThreadLocalRequest > ().getSession().getId(). Unfortunately, for first time of calling, I > usually get different return strings. > > The code is bellow. I try to get session ids three times but app > returns three different strings of session id when the system (server > and client) has just started. Those strings are unique only when I > refresh browser. > > Can you help me to point out what I am missing and/or the solution? > > Thanks a lot for any help. > > Code: > > package com.mysite.testsession.client; > > import com.google.gwt.core.client.EntryPoint; > import com.google.gwt.core.client.GWT; > import com.google.gwt.user.client.rpc.AsyncCallback; > import com.google.gwt.user.client.ui.Label; > import com.google.gwt.user.client.ui.RootPanel; > import com.google.gwt.user.client.ui.VerticalPanel; > > public class GetSession implements EntryPoint { > private final GetSessionServiceAsync myService = GWT.create > (GetSessionService.class); > > public void onModuleLoad() { > final Label sessionLabel1 = new Label("Session1: "); > final Label sessionLabel2 = new Label("Session2: "); > final Label sessionLabel3 = new Label("Session3: "); > > VerticalPanel vPanel = new VerticalPanel(); > vPanel.add(sessionLabel1); > vPanel.add(sessionLabel2); > vPanel.add(sessionLabel3); > > RootPanel.get().add(vPanel); > > myService.getSessionId(new AsyncCallback<String>() { > @Override > public void onFailure(Throwable caught) { } > > @Override > public void onSuccess(String result) { > sessionLabel1.setText("Session1: " + result); > } > > }); > > myService.getSessionId(new AsyncCallback<String>() { > @Override > public void onFailure(Throwable caught) { } > > @Override > public void onSuccess(String result) { > sessionLabel2.setText("Session2: " + result); > } > }); > myService.getSessionId(new AsyncCallback<String>() { > @Override > public void onFailure(Throwable caught) { } > > @Override > public void onSuccess(String result) { > sessionLabel3.setText("Session3: " + result); > } > }); > } > > } > > -------------------------- > package com.mysite.testsession.client; > > import com.google.gwt.user.client.rpc.RemoteService; > import com.google.gwt.user.client.rpc.RemoteServiceRelativePath; > > @RemoteServiceRelativePath("mysession") > public interface GetSessionService extends RemoteService { > String getSessionId();} > > ----------------------------- > package com.mysite.testsession.client; > > import com.google.gwt.user.client.rpc.AsyncCallback; > > public interface GetSessionServiceAsync { > void getSessionId(AsyncCallback<String> callback);} > > ---------------------------- > package com.mysite.testsession.server; > > import com.mysite.testsession.client.GetSessionService; > import com.google.gwt.user.server.rpc.RemoteServiceServlet; > > @SuppressWarnings("serial") > public class GetSessionServiceImpl extends RemoteServiceServlet > implements GetSessionService { > > @Override > public String getSessionId() { > return getThreadLocalRequest().getSession().getId(); > } > > > > } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en -~----------~----~----~----~------~----~------~--~---
