How to handle refresh event?

34 views
Skip to first unread message

Diyko

unread,
Aug 19, 2008, 11:01:46 AM8/19/08
to Google Web Toolkit
I have to develop following behaviour
1) Application invalidates session, when I close browser
2) When I refresh page, application does nothing

could Somebody help me?

I tried to use close listener but it works in both case on close and
on refresh

Window.addWindowCloseListener(new WindowCloseListener(){

public void onWindowClosed() {
authService.logout(new DefaultAsyncCallback<Object>());
}

public String onWindowClosing() {
return null;
}

});

Ian Petersen

unread,
Aug 19, 2008, 11:14:36 AM8/19/08
to Google-We...@googlegroups.com
On Tue, Aug 19, 2008 at 11:01 AM, Diyko <Diyk...@gmail.com> wrote:
> I have to develop following behaviour
> 1) Application invalidates session, when I close browser
> 2) When I refresh page, application does nothing
>
> could Somebody help me?

On the face of it, I don't think it's possible to distinguish between
a refresh and a close--in both cases the browser destroys the current
page (a refresh is just a destroy followed by a build).

You could maybe fake it with a timeout on the server:

- in the browser's onClose(), tell the server "I'm going away"
- in your module's onModuleLoad(), tell the server "I've just started"

If you store a unique token in a cookie and pass that token in both
the "I'm going away" and the "I've just started" messages, the server
could match the two messages together. If an "I've just started"
message arrives shortly an "I'm going away" message, the server could
interpret that as a refresh and not kill the session.

In other words, from the server's perspective:

- receive "I'm going away" from unique token FOO
- set timeout of 30 (?) seconds
- if you receive "I've just started" from unique token FOO before
the timeout expires, cancel the timeout
- if the timeout expires, kill the session

You'd have to set the length of the timeout to suit your situation.

Ian

Diyko

unread,
Aug 19, 2008, 12:24:09 PM8/19/08
to Google Web Toolkit
Thx for ideas

fr...@allen-sauer.com

unread,
Aug 19, 2008, 2:16:24 PM8/19/08
to Google-We...@googlegroups.com
The usual way to do this is with a session cookie which will
automatically go away when the browser process exits. However
navigating to another page or closing the tab does not cause a loss of
session. I think this is what you want as the user can hit BACK button
or recover the closed tab.
Fred

On 8/19/08, Diyko <Diyk...@gmail.com> wrote:
>
> Thx for ideas
> >
>


--
Fred Sauer
fr...@allen-sauer.com

fr...@allen-sauer.com

unread,
Aug 19, 2008, 2:17:31 PM8/19/08
to Google-We...@googlegroups.com

fr...@allen-sauer.com

unread,
Aug 19, 2008, 2:18:37 PM8/19/08
to Google-We...@googlegroups.com

fr...@allen-sauer.com

unread,
Aug 19, 2008, 2:19:42 PM8/19/08
to Google-We...@googlegroups.com

Diyko

unread,
Aug 20, 2008, 3:39:38 AM8/20/08
to Google Web Toolkit
2f...@allen-sauer.com
Please dont abuse thread

I found solution

onModuleLoad(){
setSessionTimeOut|(-1);

Window.addWindowCloseListener(new WindowCloseListener() {

public void onWindowClosed() {
setSessionTimeOut(30);
}

public String onWindowClosing() {
return null;
}
});
}

On close or refresh event i set time out to 30 seconds

if user refresh page timeout set to -1 and session does not expire
but if window are closed session will be expired in 30 seconds

walden

unread,
Aug 20, 2008, 9:54:58 AM8/20/08
to Google Web Toolkit
Diyko,

And now for something completely different...

On Aug 19, 11:01 am, Diyko <Diyko...@gmail.com> wrote:
> I have to develop following behaviour
>  1) Application invalidates session, when I close browser

Automatic. A session, like a connection, is an agreement between two
parties. Once the browser is closed, one of the parties goes away,
and so does the session by definition. I'm sure you will protest that
interpretation, but I hope you will also think it through.

> 2) When I refresh page, application does nothing

When you refresh the page, don't you want "the application" to send
out a fresh copy? What is it you don't want to happen, specifically?
If you don't want a new session cookie created, then I think you have
nothing to worry about.

>
> could Somebody help me?
>
> I tried to use close listener but it works in both case on close and
> on refresh

The bigger question: why bother with this? Your server will scale
better if it's stateless (no session), and if you really want
sessions, the default behavior offered by the Java EE Servlet
specification ("JSESSIONID" cookie) corresponds with the life of an
open browser. That's a fit for most applications.

Best,

Walden
Reply all
Reply to author
Forward
0 new messages