下面列出了javax.servlet.http.HttpSession#getCreationTime() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
PrintWriter pw = resp.getWriter();
HttpSession session = req.getSession(true);
session.invalidate();
// Ugly but the easiest way to test of the session is valid or not
boolean result;
try {
session.getCreationTime();
result = false;
} catch (IllegalStateException ise) {
result = true;
}
if (result) {
pw.print("PASS");
} else {
pw.print("FAIL");
}
}
private JSONObject sessionToJsonObject(HttpServletRequest request) {
long creationTime = 0;
long lastAccessedTime = 0;
int maxInactiveInterval = 0;
HttpSession httpSession = request.getSession(false);
if (httpSession != null) {
creationTime = httpSession.getCreationTime();
lastAccessedTime = httpSession.getLastAccessedTime();
maxInactiveInterval = httpSession.getMaxInactiveInterval();
}
JSONObject sessionInfo = new JSONObject();
sessionInfo.accumulate("creationTime", creationTime);
sessionInfo.accumulate("lastAccessedTime", lastAccessedTime);
sessionInfo.accumulate("maxInactiveInterval", maxInactiveInterval);
return sessionInfo;
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
PrintWriter pw = resp.getWriter();
HttpSession session = req.getSession(true);
session.invalidate();
// Ugly but the easiest way to test of the session is valid or not
boolean result;
try {
session.getCreationTime();
result = false;
} catch (IllegalStateException ise) {
result = true;
}
if (result) {
pw.print("PASS");
} else {
pw.print("FAIL");
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
resp.setContentType("text/plain");
resp.setCharacterEncoding("UTF-8");
PrintWriter pw = resp.getWriter();
HttpSession session = req.getSession(true);
session.invalidate();
// Ugly but the easiest way to test of the session is valid or not
boolean result;
try {
session.getCreationTime();
result = false;
} catch (IllegalStateException ise) {
result = true;
}
if (result) {
pw.print("PASS");
} else {
pw.print("FAIL");
}
}
public static long getSessionAgeSum() {
if (!instanceCreated) {
return -1;
}
final long now = System.currentTimeMillis();
long result = 0;
for (final HttpSession session : SESSION_MAP_BY_ID.values()) {
try {
result += now - session.getCreationTime();
} catch (final Exception e) {
// Tomcat can throw "java.lang.IllegalStateException: getCreationTime: Session already invalidated"
continue;
}
}
return result;
}
void unregisterSessionIfNeeded(HttpSession session) {
if (session != null) {
try {
session.getCreationTime();
// https://issues.jenkins-ci.org/browse/JENKINS-20532
// https://bugs.eclipse.org/bugs/show_bug.cgi?id=413019
session.getLastAccessedTime();
} catch (final IllegalStateException e) {
// session.getCreationTime() lance IllegalStateException si la session est invalidée
synchronized (session) {
sessionDestroyed(new HttpSessionEvent(session));
}
}
}
}
/**
* @param login
* @param session
*/
public SessionInfo(String login, HttpSession session) {
setLogin(login);
setSession(session);
secure = false;
creationTime = session.getCreationTime();
}
/**
* @param login
* @param session
*/
public SessionInfo(String login, HttpSession session) {
setLogin(login);
setSession(session);
secure = false;
creationTime = session.getCreationTime();
}
@Override
public void sessionDestroyed(HttpSessionEvent se) {
final HttpSession session = se.getSession();
final int duration = (int) ((System.currentTimeMillis() - session.getCreationTime()) / 1000);
final int inactivity = (int) ((System.currentTimeMillis() - session.getLastAccessedTime()) / 1000);
final String sessionInfo = "[id=" + se.getSession().getId() + ", timeout=" + se.getSession().getMaxInactiveInterval() + "s, duration=" + duration
+ "s, inactivity=" + inactivity + "s]";
final boolean expired = inactivity >= session.getMaxInactiveInterval();
if (expired) {
log.debug("HTTP session timed out " + sessionInfo);
} else {
log.debug("HTTP session closed " + sessionInfo);
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 如果不存在 session 会话,则创建一个 session 对象
HttpSession session = request.getSession(true);
// 获取 session 创建时间
Date createTime = new Date(session.getCreationTime());
// 获取该网页的最后一次访问时间
Date lastAccessTime = new Date(session.getLastAccessedTime());
// 设置日期输出的格式
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String title = "Servlet Session 实例";
Integer visitCount = new Integer(0);
String visitCountKey = new String("visitCount");
String userIDKey = new String("userID");
String userID = new String("admin");
// 检查网页上是否有新的访问者
if (session.isNew()) {
session.setAttribute(userIDKey, userID);
} else {
visitCount = (Integer) session.getAttribute(visitCountKey);
visitCount = visitCount + 1;
userID = (String) session.getAttribute(userIDKey);
}
session.setAttribute(visitCountKey, visitCount);
// 设置响应内容类型
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String docType = "<!DOCTYPE html>\n";
out.println(docType + "<html>\n" + "<head><title>" + title + "</title></head>\n"
+ "<body bgcolor=\"#f0f0f0\">\n" + "<h1 align=\"center\">" + title + "</h1>\n"
+ "<h2 align=\"center\">Session 信息</h2>\n" + "<table border=\"1\" align=\"center\">\n"
+ "<tr bgcolor=\"#949494\">\n" + " <th>Session 信息</th><th>值</th></tr>\n" + "<tr>\n" + " <td>id</td>\n"
+ " <td>" + session.getId() + "</td></tr>\n" + "<tr>\n" + " <td>创建时间</td>\n" + " <td>"
+ df.format(createTime) + " </td></tr>\n" + "<tr>\n" + " <td>最后访问时间</td>\n" + " <td>"
+ df.format(lastAccessTime) + " </td></tr>\n" + "<tr>\n" + " <td>用户 ID</td>\n" + " <td>" + userID
+ " </td></tr>\n" + "<tr>\n" + " <td>访问统计:</td>\n" + " <td>" + visitCount + "</td></tr>\n"
+ "</table>\n" + "</body></html>");
}
private boolean isAgeExceeded(HttpSession preferredSession, Long maxSessionAge) {
return preferredSession.getCreationTime() + maxSessionAge*1000 < System.currentTimeMillis();
}