打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
session有关问题

Session

关键字: httpsession

1.http://www.jspcn.net/htmlnews/11049329478121583.html       监听器

2.session.invalidate() ,session才会destroy

 

3.HttpSessionListener:  这个监听取不到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) {//只要一打开浏览器就会执行,没有登陆也会执行.

                   }

            public void sessionDestroyed(HttpSessionEvent event) {//只有超时,invalidate()才会执行
  
                   HttpSession se=event.getSession();
                  OnlineManager.getInstance().removeSession(se); //从列表中删除
               // System.out.println("remove session....................");//为什么浏览窗口关闭了,没有执行啊???
  
             }

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

第二种方法实现在线,下线:

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

本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
实现session监听器
[Servlet] 配置listener监听器
JAVA中文站->J动论坛:【推荐】! 网页制作技巧总结!
JSP内部对象详解
javaweb回顾第六篇谈一谈Servlet线程安全问题
jsp入门初级教程之session的使用
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服