惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Stack Overflow Blog
Stack Overflow Blog
量子位
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
小众软件
小众软件
aimingoo的专栏
aimingoo的专栏
Recent Announcements
Recent Announcements
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Security Blog
Microsoft Security Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
酷 壳 – CoolShell
酷 壳 – CoolShell
J
Java Code Geeks
V
V2EX
大猫的无限游戏
大猫的无限游戏
D
DataBreaches.Net
博客园 - Franky
爱范儿
爱范儿
T
Tailwind CSS Blog
A
About on SuperTechFans
Google DeepMind News
Google DeepMind News
博客园_首页
B
Blog RSS Feed
博客园 - 司徒正美
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知

博客园 - hone

管理 java学习 服务器相关 总统和亿万富翁经典语录(转载) js学习笔记 最近学习计划 VC控制Excel 真乱(转自csdn) 沟通(网上搜集) 工作 无题 将文档发送到打印机(From MSN) 经典68个故事 java的zip压缩(转自Jeet) 图形相关 在Oracle9i中计算时间差 六度分隔 经典故事 新年竟然第一篇?!
Manually Invalidating All Stale Sessions
hone · 2005-05-20 · via 博客园 - hone

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

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

43      else {
44        out.println("Still valid.");
45      }

46      out.println();
47    }

48  }

49}

50