1.http://www.jspcn.net/htmlnews/11049329478121583.html 監(jiān)聽器
2.session.invalidate() ,session才會destroy
3.HttpSessionListener: 這個監(jiān)聽取不到session里面的值
http://hi.baidu.com/tianshiyeben/blog/item/17d43923d695d042ad34de36.html
http://www.family168.com/tutorial/jsp/html/jsp-ch-04.html#jsp-ch-04-02 在線列表實例
下面的代碼可以獲取上線,下線的在線列表:
public class OnlineListener implements HttpSessionListener ,HttpSessionAttributeListener{
public void sessionCreated(HttpSessionEvent event) {//只要一打開瀏覽器就會執(zhí)行,沒有登陸也會執(zhí)行.
}
public void sessionDestroyed(HttpSessionEvent event) {//只有超時,invalidate()才會執(zhí)行
HttpSession se=event.getSession();
OnlineManager.getInstance().removeSession(se); //從列表中刪除
// System.out.println("remove session....................");//為什么瀏覽窗口關(guān)閉了,沒有執(zhí)行啊???
}
public void attributeAdded(HttpSessionBindingEvent event) {//如果登陸成功,就把上線用戶添加到列表.
HttpSession se=event.getSession();
String name=event.getName();
String value=(String)event.getValue();
if("username".equals(name)){
OnlineManager.getInstance().addSession(se); //添加
}
}
}
public class OnlineManager {
private static OnlineManager om;
private Map<String,HttpSession> sessions;
private OnlineManager(){
sessions=new HashMap<String,HttpSession>();//為什么沒有共用一個sessions;
}
public static OnlineManager getInstance(){
if(om==null){
om=new OnlineManager();
}
return om;
}
public void addSession(HttpSession se){
String key=(String)se.getAttribute("username");
sessions.put(key, se);
System.out.println("add 1 : "+sessions.size());
}
public void removeSession(HttpSession se){
String key=(String)se.getAttribute("username");
//sessions.remove(key); //這個只是把key=null
sessions.remove(sessions.get(key));
System.out.println("remove 1 : " +"key:"+key+sessions.size());
System.out.println(sessions);
}
}
------------------------
第二種方法實現(xiàn)在線,下線:
public class BindSession implements HttpSessionBindingListener {
private String username;
public BindSession(String username){
this.username=username;
}
public void valueBound(HttpSessionBindingEvent event) {
HttpSession session = event.getSession();
// String name=(String)session.getAttribute("name");
ServletContext application = session.getServletContext();
// 把用戶名放入在線列表
List onlineUserList = (List) application.getAttribute("onlineUserList");
// 第一次使用前,需要初始化
if (onlineUserList == null) {
onlineUserList = new ArrayList();
application.setAttribute("onlineUserList", onlineUserList);
}
onlineUserList.add(this.username);
System.out.println("valueBound: .........."+onlineUserList.size());
}
public void valueUnbound(HttpSessionBindingEvent event) {
HttpSession session = event.getSession();
// String name=(String)session.getAttribute("name");
ServletContext application = session.getServletContext();
// 從在線列表中刪除用戶名
List onlineUserList = (List) application.getAttribute("onlineUserList");
onlineUserList.remove(this.username);
System.out.println(this.username + "退出。");
}
}
public class Login extends HttpServlet {
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String name=req.getParameter("name");
String pass=req.getParameter("password");
boolean isLogin=false;
int len=set.size();
for(int i=0;i<len;i++){
if(set.containsKey(name)&&set.containsValue(pass)){
isLogin=true;
}
}
if(isLogin){
req.getSession().setAttribute("username", name);
System.out.println("login ...username="+name);
//BindListener的使用:
BindSession bl=new BindSession(name);
req.getSession().setAttribute("lis", bl);
resp.sendRedirect("index.jsp");
}
else{
resp.sendRedirect("login.jsp");
}