通过调用 jsp 发送心跳不会使会话保持活动状态
我想在浏览器上打开网页时保持会话处于活动状态,即使我很长时间没有接触它,这就是我尝试做的事情:
- 在服务器端(tomcat),会话超时设置为 8 分钟。
- 主jsp生成一个html。在此 html 中,我使用计时器 & ajax 每 3 分钟调用一次
heartbeat.jsp
还将初始会话 ID 的值传递给它。 - 在
heartbeat.jsp
中,我转储session.getId()
的值 &初始会话 ID。
[执行结果时间线]:
10:00
我打开页面 -- 主jsp程序生成客户端html10:03
heartbeat.jsp
调用 - -session.getId()
= 调用的初始会话 ID10:06
heartbeat.jsp
--session.getId() = 调用的初始会话 ID
10:09
heartbeat.jsp
--session.getId()
<>初始会话 ID
初始会话 ID 的最后访问时间为 10:06
,因此初始会话 ID 的超时时间假定为 10:14
。但为什么在 10:09
时,session.getId()
会得到另一个新 ID?
似乎前两个调用没有重置服务器端初始会话的超时计数器。我在 IE 和 Firefox 中对此进行了测试,它们具有相同的结果
我怎样才能保持会话保持活动状态?
[主要jsp代码段]:
<script type="text/javascript" src="/fp/js/prototype.js"></script>
<script type="text/javascript" src="/fp/js/jquery.min.js"></script>
<script type="text/javascript" src="/fp/js/jquery.roundabout.min.js"></script>
<script type="text/javascript">
function heartbeats(sid) {
var url = '/test/heartbeat.jsp' + '?rn=' + Math.random() +'&mysid='+sid;
var myAjax = new Ajax.Request(url,{
method: 'get',
onFailure: function(request) {
alert("connect problem, can't do heartbeat!");
},
onSuccess: function(response) {
if (response.responseText.indexOf("timeout") != -1) {
alert("original session is expired");
}
}
});
}
function init_heartbeat(sid) {
new PeriodicalExecuter(function(startHeart){heartbeats(sid);}, 60*3);
}
init_heartbeat("<%=session.getId()%>");
</script>
[heartbeat.jsp代码段]:
String server_sid = (String)session.getId();
String client_sid =
request.getParameter("mysid")==null?"":(String)request.getParameter("mysid");
java.util.Calendar calendar = java.util.Calendar.getInstance();
File lf = new File("/usr/tmp/hb.log");
try {
lf.println( "servre sid = "+server_sid);
lf.println( "client sid = "+client_sid);
lf.println( "~~~~~");
lf.close();
} catch(Exception e) {
out.print(e);
}
if(!server_sid.equals(client_sid))
out.print("timeout");
I want to keep session alive when I open web page on browser even if I doesn't touch it for a long time, this is how I try to do:
- on server side (tomcat), session timeout set to 8 mins.
- main jsp generates a html. In this html, I use timer & ajax to call
heartbeat.jsp
every 3 mins & also pass the initial session id's value to it. - In
heartbeat.jsp
, I dumpsession.getId()
's value & initial session id.
[timeline of execution result]:
10:00
I open page -- main jsp program generates client html10:03
heartbeat.jsp
called --session.getId()
= initial session id10:06
heartbeat.jsp
called --session.getId()
= initial session id10:09
heartbeat.jsp
called --session.getId()
<> initial session id
last access time of inital session id is at 10:06
, so timeout time of initial session id is supposed at 10:14
. But why at 10:09
, session.getId()
get another new id?
It seems first 2 calls didn't reset intial session's timeout counter on server side. I test this both in IE and Firefox, they have same result
How can I keep session alive for good?
[the main jsp code seg]:
<script type="text/javascript" src="/fp/js/prototype.js"></script>
<script type="text/javascript" src="/fp/js/jquery.min.js"></script>
<script type="text/javascript" src="/fp/js/jquery.roundabout.min.js"></script>
<script type="text/javascript">
function heartbeats(sid) {
var url = '/test/heartbeat.jsp' + '?rn=' + Math.random() +'&mysid='+sid;
var myAjax = new Ajax.Request(url,{
method: 'get',
onFailure: function(request) {
alert("connect problem, can't do heartbeat!");
},
onSuccess: function(response) {
if (response.responseText.indexOf("timeout") != -1) {
alert("original session is expired");
}
}
});
}
function init_heartbeat(sid) {
new PeriodicalExecuter(function(startHeart){heartbeats(sid);}, 60*3);
}
init_heartbeat("<%=session.getId()%>");
</script>
[heartbeat.jsp code seg]:
String server_sid = (String)session.getId();
String client_sid =
request.getParameter("mysid")==null?"":(String)request.getParameter("mysid");
java.util.Calendar calendar = java.util.Calendar.getInstance();
File lf = new File("/usr/tmp/hb.log");
try {
lf.println( "servre sid = "+server_sid);
lf.println( "client sid = "+client_sid);
lf.println( "~~~~~");
lf.close();
} catch(Exception e) {
out.print(e);
}
if(!server_sid.equals(client_sid))
out.print("timeout");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我通过定时后台 jquery ajax 调用空白页面解决了保持会话活动的问题 - 然后服务器在后台每 x 秒收到一个请求。 Jquery可能更简单?
I solved this keep session alive issue with a timed background jquery ajax call to a blank page - The server receives a request every x seconds in the background then. Jquery may be simpler?