Friday, January 20, 2012

ZK Flow Control - Prevent Opening Multiple Tabs

Sometimes we want limit that one user can only active one page, how to achieve this?
There are several parts described as follows:

1. The entry page


    <button label="go to page 1">
        <attribute name="onClick">
            // get the user active time
            Long time = test.Maps.keyMap.get("userName");
            // If no active time or expired, set active time and redirect,
            // Stay at this page otherwise.
            if (time == null || (System.currentTimeMillis()-time) > 10000) {
                test.Maps.keyMap.put("userName", System.currentTimeMillis());
                test.Maps.tmpKeyMap.put("userName", test.Maps.keyMap.get("userName"));
            else // redirect to another error page as need here

The entry page is the only page that a user can open multiple times, we check whether the user is already work at other page while 'go to page 1' button clicked, redirect to 'page1.zul' or stay at this page as need.

Note we put the active time into another tmpKeyMap before redirect to page1, we will check it at page1 later, we can not check the keyMap directly because the result will the same at different page/session.

2. Page 1


    <div visible="false">
        <attribute name="onCreate">
            // get active time from tmpKeyMap and check it
            Long time = test.Maps.tmpKeyMap.get("userName");
            if (time == null || (System.currentTimeMillis()-time) > 10000)
        <button label="go to page 2">
            <attribute name="onClick">
                test.Maps.tmpKeyMap.put("userName", test.Maps.keyMap.get("userName"));
    <timer repeats="true" running="true" delay="1000">
        <attribute name="onTimer">
            // keep update the active time and clear the data in tmpKeyMap
            test.Maps.keyMap.put("userName", System.currentTimeMillis());
            if (test.Maps.tmpKeyMap.get("userName") != null)

In this page, the content is initial invisible, we get the active time from tmpKeyMap and show the content if it is valid, redirect to test.zul otherwise.

Then we start a timer, keep update the active time, the delay is 1000ms and remember we consider that time as expired if (current time - active time) > 10000ms (10 seconds). You can adjust the delay time and expire condition as need, the larger the delay and expire condition, the longer the user have to wait to enter the page again after he/she left.

The effect:

1. The user can not link to page1.zul directly
2. Only one page can redirect to page1.zul from test.zul
3. Click the browser's 'previous page' or refresh page will return to test.zul.
4. Can enter page1 again after return to test.zul and wait about 10 seconds.

You can download the full project from github:


  1. Hi did you write something for ZK+JasperReport

    1. yes, just haven't made an article of it, I'll do it later

    2. done,