码迷,mamicode.com
首页 > Web开发 > 详细

关于使用HttpSessionBindingListener获取在线用户数,同一用户登陆一次

时间:2014-12-15 12:01:19      阅读:244      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   使用   sp   

  原创地址:http://blog.csdn.net/jiaoxueli/article/details/2226134

 

  考虑到项目中统计在线用户数量和同一用户只能登陆一次的需求,查询联系 HttpSessionBindingListener接口的使用,记录以备后用,也供同样需要的同仁参考。

下面为我的测试例子,首先建个web工程,例子中程序包括:OnLineUser.java  ,login.jsp ,logout.jsp,onLineUser.jsp四个文件

OnLineUser.java清单:

package pub;
bubuko.com,布布扣
bubuko.com,布布扣/*
bubuko.com,布布扣 * onLineUser类实现HttpSessionBindingListener接口
bubuko.com,布布扣 * onLineUser类将具有HttpSessionBindingListener接口的特有属性
bubuko.com,布布扣 * 那么HttpSessionBindingListener接口的特有属性是什么呢?
bubuko.com,布布扣 * HttpSessionBindingListener接口具有的两个空函数
bubuko.com,布布扣 * public void valueBound(HttpSessionBindingEvent e)
bubuko.com,布布扣 * public void valueUnBound(HttpSessionBindingEvent e)
bubuko.com,布布扣 * 
bubuko.com,布布扣 * onLineUser实现一些方法:获取在线用户数
bubuko.com,布布扣 * 
bubuko.com,布布扣 */
bubuko.com,布布扣
bubuko.com,布布扣import javax.servlet.http.*; 
bubuko.com,布布扣import java.util.*; 
bubuko.com,布布扣
bubuko.com,布布扣public class OnLineUser implements HttpSessionBindingListener 
bubuko.com,布布扣   
bubuko.com,布布扣   public OnLineUser(){
bubuko.com,布布扣   } 
bubuko.com,布布扣
bubuko.com,布布扣   private Vector users=new Vector();
bubuko.com,布布扣   public int getCount(){
bubuko.com,布布扣       users.trimToSize();//调整Vector users的容量为Vector users的大小
bubuko.com,布布扣       return users.capacity();//返回users的容量
bubuko.com,布布扣   }
bubuko.com,布布扣   public boolean existUser(String userName){
bubuko.com,布布扣       users.trimToSize();
bubuko.com,布布扣       boolean existUser=false;
bubuko.com,布布扣       for (int i=0;i<users.capacity();i++ )
bubuko.com,布布扣       {
bubuko.com,布布扣           if (userName.equals((String)users.get(i)))
bubuko.com,布布扣           {
bubuko.com,布布扣               existUser=true;
bubuko.com,布布扣               break;
bubuko.com,布布扣           }
bubuko.com,布布扣       }
bubuko.com,布布扣       return existUser;
bubuko.com,布布扣   }
bubuko.com,布布扣
bubuko.com,布布扣   public boolean deleteUser(String userName) {
bubuko.com,布布扣       users.trimToSize();
bubuko.com,布布扣       if(existUser(userName)){
bubuko.com,布布扣           int currUserIndex=-1;
bubuko.com,布布扣           for(int i=0;i<users.capacity();i++){
bubuko.com,布布扣               if(userName.equals((String)users.get(i))){
bubuko.com,布布扣                   currUserIndex=i;
bubuko.com,布布扣                   break;
bubuko.com,布布扣               }
bubuko.com,布布扣           }
bubuko.com,布布扣           if (currUserIndex!=-1){
bubuko.com,布布扣               users.remove(currUserIndex);
bubuko.com,布布扣               users.trimToSize();
bubuko.com,布布扣               return true;
bubuko.com,布布扣           }
bubuko.com,布布扣       }
bubuko.com,布布扣       return false;
bubuko.com,布布扣   }
bubuko.com,布布扣
bubuko.com,布布扣   public Vector getOnLineUser()
bubuko.com,布布扣   {
bubuko.com,布布扣       return users;
bubuko.com,布布扣   }
bubuko.com,布布扣   
bubuko.com,布布扣   public void valueBound(HttpSessionBindingEvent e) 
bubuko.com,布布扣       users.trimToSize();
bubuko.com,布布扣       System.out.println("请求:::::::::::"+e.getName());
bubuko.com,布布扣    if(!existUser(e.getName())){
bubuko.com,布布扣        users.add(e.getName());
bubuko.com,布布扣        System.out.print(e.getName()+"    登入到系统 "+(new Date()));
bubuko.com,布布扣        System.out.println("      在线用户数为:"+getCount());
bubuko.com,布布扣    }else{
bubuko.com,布布扣        System.out.println(e.getName()+"已经存在");
bubuko.com,布布扣        }
bubuko.com,布布扣   } 
bubuko.com,布布扣 
bubuko.com,布布扣   public void valueUnbound(HttpSessionBindingEvent e) 
bubuko.com,布布扣    users.trimToSize();
bubuko.com,布布扣    String userName=e.getName();
bubuko.com,布布扣    deleteUser(userName);
bubuko.com,布布扣    System.out.print(userName+"    退出系统 "+(new Date()));
bubuko.com,布布扣    System.out.println("      在线用户数为:"+getCount());
bubuko.com,布布扣   } 
bubuko.com,布布扣   
bubuko.com,布布扣   
bubuko.com,布布扣}

 

 

login.jsp 清单:

<%@ page contentType="text/html;charset=gb2312" %> 
bubuko.com,布布扣<html>
bubuko.com,布布扣<head>
bubuko.com,布布扣<title>测试HttpSessionBindingListener接口</title>
bubuko.com,布布扣</head>
bubuko.com,布布扣
bubuko.com,布布扣<body>
bubuko.com,布布扣<form name="login" method="post" action="onLineUser.jsp">
bubuko.com,布布扣<input type=text name="username">
bubuko.com,布布扣<input type=submit name="submit" value="登陆">
bubuko.com,布布扣</form>
bubuko.com,布布扣</body>
bubuko.com,布布扣</html>

 

 

logout.jsp清单:

 

<%@ page contentType="text/html;charset=gb2312" %> 
bubuko.com,布布扣<%@ page import="pub.OnLineUser,java.util.*" %> 
bubuko.com,布布扣<jsp:useBean id="onlineuser" class="pub.OnLineUser" scope="application"/>
bubuko.com,布布扣<html> 
bubuko.com,布布扣<head> 
bubuko.com,布布扣<title>搞定JSP在线人数</title>
bubuko.com,布布扣</head>
bubuko.com,布布扣<body> 
bubuko.com,布布扣<center> 
bubuko.com,布布扣  <h1>登陆成功,欢迎您访问!</h1>
bubuko.com,布布扣</center>
bubuko.com,布布扣<% 
bubuko.com,布布扣   String username=request.getParameter("username");
bubuko.com,布布扣   if(username!=null&&onlineuser.deleteUser(username)){
bubuko.com,布布扣       out.println(username+"已经退出系统!");
bubuko.com,布布扣       session.removeAttribute(username);
bubuko.com,布布扣       out.println("<script>window.location="login.jsp";</script>");
bubuko.com,布布扣   }else{
bubuko.com,布布扣       out.println(username+"已经退出系统!");
bubuko.com,布布扣       out.println("<script>window.location="login.jsp";</script>");
bubuko.com,布布扣   }
bubuko.com,布布扣%> 
bubuko.com,布布扣<center> 
bubuko.com,布布扣  <p>elapsed制作</p>
bubuko.com,布布扣  <p> </p>
bubuko.com,布布扣  <p><a href="logout.jsp">退出系统</a></p>
bubuko.com,布布扣</center> 
bubuko.com,布布扣</body> 
bubuko.com,布布扣</html>

 

 

 

onLineUser.jsp清单

 

<%@ page contentType="text/html;charset=gb2312" %> 
bubuko.com,布布扣<%@page import="java.util.*,pub.*"%>
bubuko.com,布布扣<jsp:useBean id="onlineuser" class="pub.OnLineUser" scope="application" />
bubuko.com,布布扣<html> 
bubuko.com,布布扣<head> 
bubuko.com,布布扣<title>搞定JSP在线人数</title>
bubuko.com,布布扣</head>
bubuko.com,布布扣<body> 
bubuko.com,布布扣<center> 
bubuko.com,布布扣  <h1>登陆成功,欢迎您访问!</h1>
bubuko.com,布布扣</center>
bubuko.com,布布扣<%   session = request.getSession(false); %> 
bubuko.com,布布扣<% 
bubuko.com,布布扣   String username=request.getParameter("username");
bubuko.com,布布扣   if (onlineuser.existUser(username)){
bubuko.com,布布扣       out.println("用户<font color=red>"+username+"</font>已经登陆!");
bubuko.com,布布扣   }else{
bubuko.com,布布扣       session.setMaxInactiveInterval(600);//Sesion有效时长,以秒为单位
bubuko.com,布布扣       session.setAttribute(username,onlineuser); 
bubuko.com,布布扣       out.println("欢迎新用户:<font color=red>"+username+"</font>登陆到系统!");
bubuko.com,布布扣   }
bubuko.com,布布扣   out.println("<br>当前在线用户人数:<font color=red>"+onlineuser.getCount()+"</font><br>");
bubuko.com,布布扣   Vector vt=onlineuser.getOnLineUser();
bubuko.com,布布扣   Enumeration e = vt.elements();
bubuko.com,布布扣   out.println("在线用户列表");
bubuko.com,布布扣   out.println("<table border=1>");
bubuko.com,布布扣   out.println("<tr><td>用户名</td></tr>");
bubuko.com,布布扣   /*while(e.hasMoreElements()){
bubuko.com,布布扣       out.println("<tr><td>");
bubuko.com,布布扣       out.println((String)e.nextElement()+"<br>");
bubuko.com,布布扣       out.println("</td></tr>");
bubuko.com,布布扣   }
bubuko.com,布布扣   下面的方法也是可以的,这两种方法都是可行的,其实Iterator是Enumeration的子类
bubuko.com,布布扣   */
bubuko.com,布布扣   for(Iterator it=vt.iterator();it.hasNext();){
bubuko.com,布布扣       out.println("<tr><td>");
bubuko.com,布布扣       out.println((String)it.next()+"<br>");
bubuko.com,布布扣       out.println("</td></tr>");
bubuko.com,布布扣   }
bubuko.com,布布扣   out.println("</table>");
bubuko.com,布布扣     
bubuko.com,布布扣%> 
bubuko.com,布布扣<center> 
bubuko.com,布布扣  <p>elapsed制作</p>
bubuko.com,布布扣  <p> </p>
bubuko.com,布布扣<%
bubuko.com,布布扣   out.println("<p><a href=‘logout.jsp?username="+username+"‘>退出系统</a></p>");
bubuko.com,布布扣%>
bubuko.com,布布扣</center> 
bubuko.com,布布扣</body> 
bubuko.com,布布扣</html>

 

 

该监听器不需要在web.xml中配置listener,完成后部署启动,访问login.jsp测试即可。

 

关于使用HttpSessionBindingListener获取在线用户数,同一用户登陆一次

标签:style   blog   http   io   ar   color   os   使用   sp   

原文地址:http://www.cnblogs.com/zhilongblogs/p/4164445.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!