演示Session Listener的使用方法
2007-12-23 12:35:49 来源:WEB开发网核心提示:import java.util.Date;import javax.servlet.http.Httpsession;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;public clas
import java.util.Date;
import javax.servlet.http.Httpsession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class SessionListen implements HttpSessionListener {
PRivate int sessionCount;
public SessionListen() {
this.sessionCount = 0;
}
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
session.setMaxInactiveInterval(60);
synchronized (this) {
sessionCount++;
}
String id = session.getId();
Date now = new Date();
String message = new StringBuffer("New Session created on ").append(
now.toString()).append("\nID: ").append(id).append("\n")
.append("There are now ").append("" + sessionCount).append(
" live sessions in the application.").toString();
System.out.println(message);
}
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
String id = session.getId();
synchronized (this) {
--sessionCount;
}
String message = new StringBuffer("Session destroyed"
+ "\nValue of destroyed session ID is").append("" + id).append(
"\n").append("There are now ").append("" + sessionCount)
.append(" live sessions in the application.").toString();
System.out.println(message);
}
}
import javax.servlet.http.Httpsession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;
public class SessionListen implements HttpSessionListener {
PRivate int sessionCount;
public SessionListen() {
this.sessionCount = 0;
}
public void sessionCreated(HttpSessionEvent se) {
HttpSession session = se.getSession();
session.setMaxInactiveInterval(60);
synchronized (this) {
sessionCount++;
}
String id = session.getId();
Date now = new Date();
String message = new StringBuffer("New Session created on ").append(
now.toString()).append("\nID: ").append(id).append("\n")
.append("There are now ").append("" + sessionCount).append(
" live sessions in the application.").toString();
System.out.println(message);
}
public void sessionDestroyed(HttpSessionEvent se) {
HttpSession session = se.getSession();
String id = session.getId();
synchronized (this) {
--sessionCount;
}
String message = new StringBuffer("Session destroyed"
+ "\nValue of destroyed session ID is").append("" + id).append(
"\n").append("There are now ").append("" + sessionCount)
.append(" live sessions in the application.").toString();
System.out.println(message);
}
}
(出处:http://www.cncms.com)
[]
更多精彩
赞助商链接