Session management in the gwt

2019-03-06 02:36发布

I am working on an GWT-ext application. In this application I managed client side session. For this I write below code:

To manage the session: import com.google.gwt.user.client.Timer;

public class ClientTimers {

    private static final Timer SESSION_MAY_HAVE_EXPIRED_TIMER = new Timer() {

        @Override
        public void run() {
            // Warn the user, that the session may have expired.
            // You could then show a login dialog, etc...
        }
    };

    public static void renewSessionTimer() {

        // First cancel the previous timer
        SESSION_MAY_HAVE_EXPIRED_TIMER.cancel();

        // Schedule again in 5 minutes (maybe make that configurable?)
        // Actually, let's subtract 10 seconds from that, because our timer
        // won't be synchronized perfectly with the server's timer.
        SESSION_MAY_HAVE_EXPIRED_TIMER.schedule(5 * 60 * 1000 - 10000);
    }
}

To get the user activity:

    Ext.get("pagePanel").addListener("click", new EventCallback() {
        @Override
        public void execute(EventObject e) {
            //MessageBox.alert("On Mouse Click");
    });

    Ext.get("pagePanel").addListener("keydown", new EventCallback() {

        @Override
        public void execute(EventObject e) { //
            //MessageBox.alert("On Key Press Click");
        }
    });

This code is working fine but my issues : This code will do log out automatically as the time out occurs.For my code I want that on click or key down it should do logout. Case is like this:If user is logged in and time to log out is 5 min.User don't do any activity on the screen than right now as per the above code it will log out automatically as the 5 min complete.

Now my requirement is that if user logged in and it doesn't do any thing for 5 mins.It should not do any logged out automatically.Instead of logging out on completion of 5 min,If user do click or key down on 6 min then it should do the log out process.

Basically the log out process as the timer exceed the specified time should be done on the user activity, not automatically.

标签: java gwt gwt-ext
3条回答
2楼-- · 2019-03-06 02:44

In the Timer, increment a variable for each second. And when user click on any button after 5 minutes or on 6th minute than check the counter variable and if the variable is greater than 6 than you can use Window.Location.reload(); to logout or reload().

查看更多
\"骚年 ilove
3楼-- · 2019-03-06 02:44

Install a JavaScript event handler on an invisible div that covers the whole area. If it gets an event, send an AJAX request to the server.

The server can then do whatever it needs to do. On the client side, you can wait for a reply from the AJAX request and display "You have been logged out".

There is one drawback of this approach: Objects stored in the session will be kept alive for too long. So even if the user never logs out and just walks away (or his browser crashes), the session will stay alive.

After a couple of days, so many dead sessions will accumulate that your server will crash.

So the better solution is to auto logout the user as you do already and install an AJAX event handler as described above to display a message when the user returns to the browser.

This way, your server can clean up dead sessions and the user gets a message when he can read it (when he is in front of the screen).

Note that you can't differentiate between the user and the cleaning crew hitting the mouse.

查看更多
在下西门庆
4楼-- · 2019-03-06 02:50

I think the thing you are searchin for is:

Window.Location.reload();

Fire it every few secons with a timer, so the user always apper to be active.

(Btw I have that from Window close issues in gwt -ext )

查看更多
登录 后发表回答