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

推荐订阅源

罗磊的独立博客
The GitHub Blog
The GitHub Blog
Hugging Face - Blog
Hugging Face - Blog
博客园 - 聂微东
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
IT之家
IT之家
小众软件
小众软件
博客园_首页
G
Google Developers Blog
Apple Machine Learning Research
Apple Machine Learning Research
MyScale Blog
MyScale Blog
Engineering at Meta
Engineering at Meta
Jina AI
Jina AI
酷 壳 – CoolShell
酷 壳 – CoolShell
人人都是产品经理
人人都是产品经理
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
D
Docker
B
Blog
雷峰网
雷峰网
WordPress大学
WordPress大学
Stack Overflow Blog
Stack Overflow Blog
宝玉的分享
宝玉的分享

博客园 - 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