鼠标在GWT中移动会话超时 [英] Mouse movement for session timeout in GWT
问题描述
我写了一个类,其中使用了GWT会话对象。
public static boolean ValidSession(HttpSession session){
boolean aResult = true;
logger.debug(Start of ValidSession);
尝试
{
if(session!= null)
{
String strUserInf =;
strUserInf =(String)session.getAttribute(userInf);
logger.debug(用户Inf在会话中:+ strUserInf);
if(strUserInf == null || strUserInf.trim()。equals())
{
logger.debug(User Info blank);
aResult = false;
}
}
else
{
logger.debug(SessionNull);
aResult = false;
$ b $ catch(Exception e)
{
logger.error(ValidSession中的异常:,e);
aResult = false;
}
logger.debug(End of isSessionValid);
返回aResult;
}
public static void TimeUpdate(HttpSession session){
session.setAttribute(lastAccessed,String.valueOf(session.getLastAccessed()));
System.out.println(lastAccessed+ session.getAttribute(lastAccessed));
}
但它只在RPC调用中重置定时器。我想检测鼠标移动。
任何人都可以提出解决方案吗?提前致谢。
您可以在模块加载时执行类似的代码。
创建一个每2分钟运行一次的计时器,并设置会话过期时间戳:
private static Timer SESSION_TIMER = new Timer(){
public void run(){
SessionFactory.putValue(SesisonKey.SESSION_EXPIRED,
true);
}
};
调用此方法在模块加载中使用此方法更新计时器会话,如下所示:
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分钟= 60000毫秒
CLIENT_SIDE_TIMER.schedule(60000 * 2);
$ b NativePreviewHandler处理程序捕获鼠标事件并检查会话是否为
final NativePreviewHandler nativeHandler = new NativePreviewHandler(){
@Override
public void onPreviewNativeEvent(NativePreviewEvent事件){
preventBack();
$ b $ if SessionFactory
.getValue(SesisonKey.CLIENT_SESSION_EXPIRED);
if(expire){
boolean show = false;
//注销会话代码
ClientSideTimers.renewSessionTimer();
}
}
}
};
Event.addNativePreviewHandler(nativeHandler);
如果您的屏幕稳定2分钟,则会通过定时器设置过期会话变量值false,是假的,然后做注销。
SESSION_EXPIRED是会话过期检查的一个常量变量不仅仅是SessionFactory是我的自定义工厂代码。你只是忽略了那些使用了我的项目模式的变量。你只需要设置一个常量就可以了。如果还有任何查询,请告诉我。
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.
解决方案 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.
这篇关于鼠标在GWT中移动会话超时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!