標籤: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;
/*
* onLineUser類實現HttpSessionBindingListener介面
* onLineUser類將具有HttpSessionBindingListener介面的特有屬性
* 那麼HttpSessionBindingListener介面的特有屬性是什麼呢?
* HttpSessionBindingListener介面具有的兩個空函數
* public void valueBound(HttpSessionBindingEvent e)
* public void valueUnBound(HttpSessionBindingEvent e)
*
* onLineUser實現一些方法:擷取線上使用者數
*
*/
import javax.servlet.http.*;
import java.util.*;
public class OnLineUser implements HttpSessionBindingListener {
public OnLineUser(){
}
private Vector users=new Vector();
public int getCount(){
users.trimToSize();//調整Vector users的容量為Vector users的大小
return users.capacity();//返回users的容量
}
public boolean existUser(String userName){
users.trimToSize();
boolean existUser=false;
for (int i=0;i<users.capacity();i++ )
{
if (userName.equals((String)users.get(i)))
{
existUser=true;
break;
}
}
return existUser;
}
public boolean deleteUser(String userName) {
users.trimToSize();
if(existUser(userName)){
int currUserIndex=-1;
for(int i=0;i<users.capacity();i++){
if(userName.equals((String)users.get(i))){
currUserIndex=i;
break;
}
}
if (currUserIndex!=-1){
users.remove(currUserIndex);
users.trimToSize();
return true;
}
}
return false;
}
public Vector getOnLineUser()
{
return users;
}
public void valueBound(HttpSessionBindingEvent e) {
users.trimToSize();
System.out.println("請求:::::::::::"+e.getName());
if(!existUser(e.getName())){
users.add(e.getName());
System.out.print(e.getName()+" 登入到系統 "+(new Date()));
System.out.println(" 線上使用者數為:"+getCount());
}else{
System.out.println(e.getName()+"已經存在");
}
}
public void valueUnbound(HttpSessionBindingEvent e) {
users.trimToSize();
String userName=e.getName();
deleteUser(userName);
System.out.print(userName+" 退出系統 "+(new Date()));
System.out.println(" 線上使用者數為:"+getCount());
}
}
login.jsp 清單:
<%@ page contentType="text/html;charset=gb2312" %>
<html>
<head>
<title>測試HttpSessionBindingListener介面</title>
</head>
<body>
<form name="login" method="post" action="onLineUser.jsp">
<input type=text name="username">
<input type=submit name="submit" value="登陸">
</form>
</body>
</html>
logout.jsp清單:
<%@ page contentType="text/html;charset=gb2312" %>
<%@ page import="pub.OnLineUser,java.util.*" %>
<jsp:useBean id="onlineuser" class="pub.OnLineUser" scope="application"/>
<html>
<head>
<title>搞定JSP線上人數</title>
</head>
<body>
<center>
<h1>登陸成功,歡迎您訪問!</h1>
</center>
<%
String username=request.getParameter("username");
if(username!=null&&onlineuser.deleteUser(username)){
out.println(username+"已經退出系統!");
session.removeAttribute(username);
out.println("<script>window.location="login.jsp";</script>");
}else{
out.println(username+"已經退出系統!");
out.println("<script>window.location="login.jsp";</script>");
}
%>
<center>
<p>elapsed製作</p>
<p> </p>
<p><a href="logout.jsp">退出系統</a></p>
</center>
</body>
</html>
onLineUser.jsp清單
<%@ page contentType="text/html;charset=gb2312" %>
<%@page import="java.util.*,pub.*"%>
<jsp:useBean id="onlineuser" class="pub.OnLineUser" scope="application" />
<html>
<head>
<title>搞定JSP線上人數</title>
</head>
<body>
<center>
<h1>登陸成功,歡迎您訪問!</h1>
</center>
<% session = request.getSession(false); %>
<%
String username=request.getParameter("username");
if (onlineuser.existUser(username)){
out.println("使用者<font color=red>"+username+"</font>已經登陸!");
}else{
session.setMaxInactiveInterval(600);//Sesion有效時間長度,以秒為單位
session.setAttribute(username,onlineuser);
out.println("歡迎新使用者:<font color=red>"+username+"</font>登陸到系統!");
}
out.println("<br>當前線上使用者人數:<font color=red>"+onlineuser.getCount()+"</font><br>");
Vector vt=onlineuser.getOnLineUser();
Enumeration e = vt.elements();
out.println("線上使用者列表");
out.println("<table border=1>");
out.println("<tr><td>使用者名稱</td></tr>");
/*while(e.hasMoreElements()){
out.println("<tr><td>");
out.println((String)e.nextElement()+"<br>");
out.println("</td></tr>");
}
下面的方法也是可以的,這兩種方法都是可行的,其實Iterator是Enumeration的子類
*/
for(Iterator it=vt.iterator();it.hasNext();){
out.println("<tr><td>");
out.println((String)it.next()+"<br>");
out.println("</td></tr>");
}
out.println("</table>");
%>
<center>
<p>elapsed製作</p>
<p> </p>
<%
out.println("<p><a href=‘logout.jsp?username="+username+"‘>退出系統</a></p>");
%>
</center>
</body>
</html>
該監聽器不需要在web.xml中配置listener,完成後部署啟動,訪問login.jsp測試即可。
關於使用HttpSessionBindingListener擷取線上使用者數,同一使用者登陸一次