Browser Refresh handling in GWT

5,415 views
Skip to first unread message

Niraj Salot

unread,
Feb 15, 2011, 11:13:51 PM2/15/11
to Google Web Toolkit
Hi All,

I would like to have below mentioned functionality in my application.

=========

Once the user is logged in , If by mistake user presses the F5 OR
refresh button , he should stay on the same page. Right now he is
redirected to login page and all his session data are lost.

I know about below mentioned functionality

Window.addWindowClosingHandler(new Window.ClosingHandler() {
@Override
public void onWindowClosing(ClosingEvent event) {
}

});
Window.addCloseHandler(new CloseHandler<Window>() {

@Override
public void onClose(CloseEvent<Window> event) {
}
});

I would not like to warn the user on this situation by writing
event.setMessage() in onWindowClosing() method. I just want to make
functionality which will feel the user that nothing has happended even
if by mistake he done browser refresh.

Let me know how I can do this

Thanks in Advance , Niraj Salot.

buz...@gmail.com

unread,
Feb 16, 2011, 8:29:42 AM2/16/11
to Google Web Toolkit
Hi,

I am aware of native (JSNI) approach doing this. Involves attaching
unload listener to window. This triggers both on refresh or "hard"
navigation to different url.

public final class MyRefreshListener {

public static void onUnload() {
detachOnUnloadListener();
// Do your stuff here
}

private static native void attachOnUnloadListener() /*-{
$wnd.onunload = function() {
@com.acme.gwt.client.impl.MyRefreshListener::onUnload()
();
}
}-*/;

private static native void detachOnUnloadListener() /*-{
$wnd.onunload = null
}-*/;

}

It could be that on newer GWT version this approach is encapsulated,
but I simply failed to find one.

Hope this helps.

Dmitry

Thomas Broyer

unread,
Feb 16, 2011, 9:49:46 AM2/16/11
to google-we...@googlegroups.com
Your issue seems to be that the user is signed-out (or not automatically signed-in, when loading the page). Given that you don't explain how login is handled (and also what you mean by "session data") in your app, I'm afraid it's impossible to  help you further.

Niraj Salot

unread,
Feb 17, 2011, 12:12:01 AM2/17/11
to Google Web Toolkit
Hi,

In My application , User is presented with Login page. Once his login
is sucessful , He is transfered to another page (with History token
mechanism). This is kind of main page of the application where he does
all his work. On sucessful login , we are maintainng some user
specific data in static variables of GWT (which in truns will be Java
Script ). Also we are maintainng History tokens in our application.

My question is very simple -> How I can , have some code which will
stop the refresh of browser and keep the user on the same page. I have
seen in gmail. Even if you refresh, you are kept on the same page.

Let me know if case of any futher question.

Thanks , Niraj.

Sky

unread,
Feb 17, 2011, 2:45:21 AM2/17/11
to google-we...@googlegroups.com
Niraj
You're problem is two fold. As already mentioned you need to be auto logging the user back in when they refresh the page. Think about how any other ajax app works, including the newer version of google groups. I'm logged in and if I hit F5 I stay logged in. There has to be a session cookie and the app checks to see if that cookie exists (then it hasn't expired) and then load up that user's user-specific data. The user shouldn't see the log in page again unless the cookie has expired. So make sure you know how and when the session cookie expires. 20 minutes since last user activity is usually the earliest a session cookie expires, but you have to choose how long it is. Nothing wrong with having an indefinitely long session (never expires) if the site doesn't contain highly sensitive information (banking, etc).

The next part of the problem is that you don't like it that you are losing not only the history state (the "page" the user had navigated to in the web app) but you are also losing the user specific data that is stored in JS variables. You need to control both of these. You need your history management to read the hash value in the URL and rebuild the site to the "page" the user had been at before the page refresh. Not exactly easy to do, but you're gonna have to get that working.

Next, you're going to have to decide how you want to maintain those variables that are in JS. There are typically 3 options. Cookies, server session/DB storage, or local storage. I don't know anything about local storage, since it's pretty brand new (I know chrome supports it, but I don't know what other browsers do, if any (the others probably do or will soon)). Cookies are easy and not a bad option depending on the usage. You could use them as temporary storage so that refreshing the page wouldn't kill all the work done by the user. But you'd need to be sure you don't leave any cookies lying around as the user navigates the app. Each "page" of your app could store values entered in cookies until they click the Save/Cancel button, at which point you delete the cookies. Make sure you delete those cookies when the user simply navigates away to another "page" on your app. Remember, cookies are sent along with any and all server requests, so if you've got 200 cookies, they are all going to go on the ride on a simple RPC and seriously increase traffic latency.

The most common solution is storing the values on the server. You can easily RPC user entered data to the server between user entries with tons of time to spare. User input is so slow compared to simple RPC service requests. The same idea as saving and deleting cookies applies here. It's just temporary session data that shouldn't persist between "pages" otherwise it's a waste of memory. Although, it's a pretty good idea to basically do away with the Save button by simply saving the user entered data straight away to the server DB, rather than just saving it as temporary session data. Then you don't even have to worry about managing the temporary data. This is what I highly recommend. But it depends on the app and what you are trying to accomplish.

Lastly, I do know of a fool proof way to prevent any attempts to leave the website you are on, including refreshes or even changing the URL in the address bar. However this is BAD and you most definitely should NOT be doing that in your scenario. You need to implement something along the lines of what I've already said, because that's how everyone else does it.

gl!

Niraj Salot

unread,
Feb 17, 2011, 3:41:58 AM2/17/11
to Google Web Toolkit
To Make it more clear for everyone.

If I Implement below mentioned code in my application.

Window.addWindowClosingHandler(new Window.ClosingHandler() {
@Override
public void onWindowClosing(ClosingEvent
event) {

event.setMessage("Are you sure ?");
}


});
Window.addCloseHandler(new CloseHandler<Window>() {


@Override
public void onClose(CloseEvent<Window> event)
{
}
});

User is presented with a Confirmation dialog and If he says stay on
this page , then Nothing happens.

I would like to know which code can be written to have the same
behaviour without having need of any confirmation dialog.

Is there any JSNI OR any way by which we can have the same behaviour
achived wihtout any dialog.

Sky

unread,
Feb 17, 2011, 3:57:46 PM2/17/11
to google-we...@googlegroups.com
No, you always get the Confirmation dialog.

Read my post carefully. I describe the correct way of doing this. You should NOT be preventing the user from leaving the page! They would not be able to go to your site and then decide to go to www.google.com, for example, in the same tab and this will piss ppl off and they won't want to go to your site.

Your gwt code simply needs to check to see if they have already logged in and then decide to NOT display the login page and instead build the site according to the History token.

You absolutely have to do it this way. There is no work around.

$ a r j i t h Pullithodi

unread,
Feb 22, 2011, 6:57:08 AM2/22/11
to google-we...@googlegroups.com
Hi All,

I donno whether I could understand the problem here completely. 
but I was also facing same kinda problem. and I was not using the History managment.
but ever since i started using it, the problem solved.
below is the code which keeps user on same page even if the user refresh the page.

in my AppController.java :- 


if ("".equals(History.getToken())) {
     
History.newItem("list");
} else {
     
History.fireCurrentHistoryState();
}


for details :-

--
Sarjith
          


--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-we...@googlegroups.com.
To unsubscribe from this group, send email to google-web-tool...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

JC

unread,
Jan 12, 2013, 8:14:27 PM1/12/13
to google-we...@googlegroups.com
The 

History.fireCurrentHistoryState(); 

works for me

Thanks

JC
To unsubscribe from this group, send email to google-web-toolkit+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages