0

How to warn the user before session timeout?

asked 2011-11-22 15:38:50 +0800

carpolva gravatar image carpolva
155 4

Hi guys!

In my ZK application I want to warn the user (displaying a message) before the session timeout.

For example, if the timeout is 5 minutes I want to warn the user at minute 4 with the following message: "Your session are about to be expired".

Is there any way to do this?

Thank you :)

delete flag offensive retag edit

6 Replies

Sort by ยป oldest newest

answered 2011-11-23 15:44:24 +0800

zippy gravatar image zippy
504 2

updated 2011-11-23 15:46:09 +0800

add a timer and method onTimer() -> do something (display alert)
if the user is not watching the screen then the user do not will see the alert xD

link publish delete flag offensive edit

answered 2011-11-23 18:24:43 +0800

carpolva gravatar image carpolva
155 4

Hi Zippy, thanks for your answer :)

I've only a question after reading your proposal, the Timer executes something every XX seconds/minutes... so the alert always will be shown no matter if the user is idle or active, am I right?. It could be a little annoying for a user that is working watching the screen jeje.

The issue is how to recognize that session is about to expire and then warn with a message?.

Regards.

link publish delete flag offensive edit

answered 2011-12-07 11:46:42 +0800

jimmyshiau gravatar image jimmyshiau
4921 5
http://www.zkoss.org/ ZK Team

Hi
I have created a sample, use a listener

public class MyExecListener implements ExecutionInit, UiLifeCycle {

	private String timerId = "timeoutNotifyTimer";

	public void init(Execution exec, Execution parent) throws Exception {
		
		Timer timer = (Timer) exec.getDesktop().getAttribute(timerId);
		if (timer != null) {
			
			if (isSendbyMsgBox(timer, exec))
				return;
			
			timer.stop();
			timer.start();
		}
	}

	private boolean isSendbyMsgBox(Timer timer, Execution exec) {
		HttpServletRequest hreq = (HttpServletRequest)exec.getNativeRequest();
		
		for (int j = 0;; ++j) {
			final String uuid = hreq.getParameter("uuid_"+j);
			if (uuid == null)
				break;
			
			if (uuid.equals(timer.getUuid()))
				return true;
		}
		return false;
	}

	public void afterComponentAttached(Component comp, Page page) {
	}

	public void afterComponentDetached(Component comp, Page prevpage) {
	}

	public void afterComponentMoved(Component parent, Component child,
			Component prevparent) {
	}

	public void afterPageAttached(Page page, Desktop desktop) {
		 Object obj = desktop.getAttribute(timerId);
		
		if (obj == null) {
			
			int tmout = desktop.getWebApp().getConfiguration()
					.getSessionMaxInactiveInterval();
			
			final Timer timer = new Timer((tmout - 2) * 1000);
			
			timer.addEventListener(Events.ON_TIMER, new EventListener() {
				public void onEvent(Event event) throws Exception {
					
					Messagebox.show("Your session are about to be expired",
						"Information", Messagebox.OK,
						Messagebox.INFORMATION, new EventListener() {
							
							@Override
							public void onEvent(Event event) throws Exception {
								if (Messagebox.ON_OK.equals(event.getName())){
									timer.start();
				                }
							}
						});
				}
			});
			timer.setPage(page);
			desktop.setAttribute(timerId, timer);
			timer.start();
		}
	}

	public void afterPageDetached(Page page, Desktop prevdesktop) {
	}

}

zk.xml

<listener>
	<listener-class>test.MyExecListener</listener-class>
</listener>

link publish delete flag offensive edit

answered 2011-12-07 19:22:59 +0800

carpolva gravatar image carpolva
155 4

Hi Jimmy!

I really appreciate your help, that's exactly what I need :D ... I hope this example will be useful for other ZK developers too.

Thank you very much.

Regards.

link publish delete flag offensive edit

answered 2012-08-24 18:52:53 +0800

adisonz gravatar image adisonz
39

but the session refreshed when the message is prompt ,

lets say my session timeout set to 30 minutes , i set it to prompt 1 minute before the session time out 29 minutes,

waited 29 minutes , message prompt with the okay button , but the session won't die after 1 minutes , it will be alive for 30 minutes because when the message is prompt , will sent a zkau request for the event onTimer, and the session just alive again.

please help .. thanks

link publish delete flag offensive edit

answered 2012-08-29 04:11:32 +0800

RichardL gravatar image RichardL
768 4

To avoid the zkau request, you might need to do it yourself in javascript. A quick google search brought up this:

http://www.velocityreviews.com/forums/t72259-warning-that-session-about-to-timeout.html

link publish delete flag offensive edit
Your reply
Please start posting your answer anonymously - your answer will be saved within the current session and published after you log in or create a new account. Please try to give a substantial answer, for discussions, please use comments and please do remember to vote (after you log in)!

[hide preview]

Question tools

Follow

RSS

Stats

Asked: 2011-11-22 15:38:50 +0800

Seen: 980 times

Last updated: Aug 29 '12

Support Options
  • Email Support
  • Training
  • Consulting
  • Outsourcing
Learn More