Manually Invalidating All Stale Sessions

 

 1 None.gif import java.io. * ;
 2 None.gifimport java.util. * ;
 3 None.gifimport javax.servlet. * ;
 4 None.gifimport javax.servlet.http. * ;
 5 None.gif
 6 ExpandedBlockStart.gifContractedBlock.gif public   class  ManualInvalidateScan extends HttpServlet  dot.gif {
 7InBlock.gif
 8InBlock.gif  public void doGet(HttpServletRequest req, HttpServletResponse res)
 9ExpandedSubBlockStart.gifContractedSubBlock.gif                               throws ServletException, IOException dot.gif{
10InBlock.gif    res.setContentType("text/plain");
11InBlock.gif    PrintWriter out = res.getWriter();
12InBlock.gif
13InBlock.gif    // Get the current session object, create one if necessary
14InBlock.gif    HttpSession dummySession = req.getSession(true);
15InBlock.gif
16InBlock.gif    // Use the session to get the session context
17InBlock.gif    HttpSessionContext context = dummySession.getSessionContext();
18InBlock.gif
19InBlock.gif    // Use the session context to get a list of session IDs
20InBlock.gif    Enumeration ids = context.getIds();
21InBlock.gif
22InBlock.gif    // Iterate over the session IDs checking for stale sessions
23ExpandedSubBlockStart.gifContractedSubBlock.gif    while (ids.hasMoreElements()) dot.gif{
24InBlock.gif      String id = (String)ids.nextElement();
25InBlock.gif      out.println("Checking " + id + "dot.gif");
26InBlock.gif      HttpSession session = context.getSession(id);
27InBlock.gif
28InBlock.gif      // Invalidate the session if it's more than a day old or has been
29InBlock.gif      // inactive for more than an hour.
30InBlock.gif      Date dayAgo = new Date(System.currentTimeMillis() - 24*60*60*1000);
31InBlock.gif      Date hourAgo = new Date(System.currentTimeMillis() - 60*60*1000);
32InBlock.gif      Date created = new Date(session.getCreationTime());
33InBlock.gif      Date accessed = new Date(session.getLastAccessedTime());
34InBlock.gif
35ExpandedSubBlockStart.gifContractedSubBlock.gif      if (created.before(dayAgo)) dot.gif{
36InBlock.gif        out.println("More than a day old, invalidated!");
37InBlock.gif        session.invalidate();
38ExpandedSubBlockEnd.gif      }

39ExpandedSubBlockStart.gifContractedSubBlock.gif      else if (accessed.before(hourAgo)) dot.gif{
40InBlock.gif        out.println("More than an hour inactive, invalidated!");
41InBlock.gif        session.invalidate();
42ExpandedSubBlockEnd.gif      }

43ExpandedSubBlockStart.gifContractedSubBlock.gif      else dot.gif{
44InBlock.gif        out.println("Still valid.");
45ExpandedSubBlockEnd.gif      }

46InBlock.gif      out.println();
47ExpandedSubBlockEnd.gif    }

48ExpandedSubBlockEnd.gif  }

49ExpandedBlockEnd.gif}

50 None.gif

转载于:https://www.cnblogs.com/hone/archive/2005/05/20/159651.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值