Mouse movement for session timeout in GWT

北慕城南 提交于 2020-01-02 11:03:43

问题


I'm working on GWT. In my application I want to add a session time out after 2 minutes when no action is performed (including mouse events).

I wrote a class in which I used a GWT session object.

public static boolean ValidSession(HttpSession session) {

        boolean aResult = true;

        logger.debug("Start of ValidSession");
        try
        {
            if(session!=null )
            {
                String strUserInf="";
                strUserInf=(String)session.getAttribute("userInf");
                logger.debug("User Inf in session is: " + strUserInf);
                if(strUserInf==null || strUserInf.trim().equals(""))
                {
                    logger.debug("User Info blank");
                    aResult =false;
                }

            }
            else
            {
                logger.debug("SessionNull");
                aResult=false;
            }
        }
        catch (Exception e) 
        {
            logger.error("Exception in ValidSession: ", e);
            aResult = false;                        
        }
        logger.debug("End of isSessionValid");
        return aResult;
    }

    public static void TimeUpdate(HttpSession session){
        session.setAttribute("lastAccessed", String.valueOf(session.getLastAccessed()));
        System.out.println("lastAccessed "+ session.getAttribute("lastAccessed"));
       }

But it resets the timer only on RPC call. I want to detect mouse movements as well.

Can anyone suggest a solution? Thanks in advance.


回答1:


You can do code something like this on on module load.

Create one timer which is running every 2 minute and set session expire flage:

    private static Timer SESSION_TIMER = new Timer() {
            public void run() {
                SessionFactory.putValue(SesisonKey.SESSION_EXPIRED,
                        true);
            }
        };

call this method for Renew the timer session as below in on module load using this method:

    public static void renewTimer() {
            if (CLIENT_SIDE_TIMER != null) {
                CLIENT_SIDE_TIMER.cancel();
                SessionFactory.getClientInstance().put(
                        SesisonKey.SESSION_EXPIRED, false);
                //CLIENT_SIDE_TIMER.schedule(250 * 60 * 20);
                //1 Minute = 60000 Milliseconds
                CLIENT_SIDE_TIMER.schedule(60000 * 2);
            }
        }

NativePreviewHandler handler catch on mouse event and check that session is expired or not flag.

 final NativePreviewHandler nativeHandler = new NativePreviewHandler() {
                    @Override
                    public void onPreviewNativeEvent(NativePreviewEvent event) {

                    preventBack();

                        if (SessionFactory
                                .getValue(SesisonKey.CLIENT_SESSION_EXPIRED) != null) {
                            boolean expire = (Boolean) SessionFactory
                                    .getValue(SesisonKey.CLIENT_SESSION_EXPIRED);
                            if (expire) {
                                boolean show= false;
                                //logout session code
                                ClientSideTimers.renewSessionTimer();
                            }
                        }
                    }
                };
                Event.addNativePreviewHandler(nativeHandler);

if your screen stable for 2 minute than it will set expire session variable value false through timer and if value is false then do logout.

SESSION_EXPIRED is a constant variable for session expired check not more that that SessionFactory is my custom factory code. U just ignore that used varibles which my project pattern. u have to just set one constant only not more than that. Let me know if still any query.



来源:https://stackoverflow.com/questions/11157273/mouse-movement-for-session-timeout-in-gwt

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!