通过调用 jsp 发送心跳不会使会话保持活动状态

发布于 2024-10-16 19:06:44 字数 2482 浏览 3 评论 0原文

我想在浏览器上打开网页时保持会话处于活动状态,即使我很长时间没有接触它,这就是我尝试做的事情:

  1. 在服务器端(tomcat),会话超时设置为 8 分钟。
  2. 主jsp生成一个html。在此 html 中,我使用计时器 & ajax 每 3 分钟调用一次 heartbeat.jsp还将初始会话 ID 的值传递给它。
  3. heartbeat.jsp 中,我转储 session.getId() 的值 &初始会话 ID。

[执行结果时间线]:

  • 10:00 我打开页面 -- 主jsp程序生成客户端html
  • 10:03 heartbeat.jsp 调用 - - session.getId() = 调用的初始会话 ID
  • 10: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:

  1. on server side (tomcat), session timeout set to 8 mins.
  2. 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.
  3. In heartbeat.jsp, I dump session.getId()'s value & initial session id.

[timeline of execution result]:

  • 10:00 I open page -- main jsp program generates client html
  • 10:03 heartbeat.jsp called -- session.getId() = initial session id
  • 10:06 heartbeat.jsp called -- session.getId() = initial session id
  • 10: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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深者入戏 2024-10-23 19:06:44

我通过定时后台 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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文