国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項超值服

開通VIP
session有關(guān)問題

Session

關(guān)鍵字: httpsession

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");
  }

本站僅提供存儲服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
實現(xiàn)session監(jiān)聽器
[Servlet] 配置listener監(jiān)聽器
JAVA中文站->J動論壇:【推薦】! 網(wǎng)頁制作技巧總結(jié)!
JSP內(nèi)部對象詳解
javaweb回顧第六篇談一談Servlet線程安全問題
jsp入門初級教程之session的使用
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長圖 關(guān)注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服