Monitor online users

The function of the Listener is to listen to the effective events of the Web container. It is managed by the Servlet container. The Listener interface is used to listen to an execution program and make appropriate response according to the needs of the program

This column applies HttpBindingListener listening interface to listen for binding information of objects in HTTP session. It's the only listening that doesn't need to be set in web.xml.

The HttpBindingListener interface provides two methods:
valueBound(HttpSessionBindingEvent arg0): automatically called when an object is added to the scope of the session
 valueUnbound(HttpSessionBindingEvent arg0): automatically transferred when an object is removed from the scope of a session



Create the LoginList.java class to store the specific operations of users and online users

public class LoginList {
	private static LoginList user=new LoginList();
	private Vector vector=null;
	public LoginList(){
		this.vector=new Vector();
	}
	public static LoginList getInstance(){
		return user;
	}
	public boolean addLoginList(String user){		//User login
		if(user!=null){
			this.vector.add(user);
			return true;
		}else{
			return false;
		}
	}
	public Vector getList(){						//Get user list
		return vector;
	}
	public void removeLoginList(String user){		//delete user
		if(user!=null){
			vector.removeElement(user);
		}
	}
}

Create LoginNote.java class and implement HttoSessionBindingListener class

public class LoginNote implements javax.servlet.http.HttpSessionBindingListener {
	private String user;
	private LoginList container=LoginList.getInstance();
	public LoginNote(){
		user="";
	}
	public void setUser(String user){
		this.user=user;
	}
	public String getUser(){
		return this.user;
	}
	@Override
	public void valueBound(HttpSessionBindingEvent arg0) {
		System.out.println(this.user+"The user is online");
	}
	@Override
	public void valueUnbound(HttpSessionBindingEvent arg0) {
		System.out.println(this.user+"The user is offline");
		if(user!=""){
			container.removeLoginList(user);
		}
	}
	

}

Create LoginList.jsp online user page

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
   <%@ page import="java.util.*" %>
   <%@ page import="com.cn.zj.Filter.*" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
   <%
    LoginList list=LoginList.getInstance();
  	LoginNote ut=new LoginNote();
  	String name=request.getParameter("user");
  	ut.setUser(name);
  	session.setAttribute("list",ut);
  	list.addLoginList(ut.getUser());
  	session.setMaxInactiveInterval(10);
    %>
<body>
<div align="center">
<table width="400" height="150" border="0" cellpadding="0" cellspacing="0" bgcolor="lightblue">
<tr align="center"><td>User online list</td></tr>
	<tr>
		<td align="center"><br>
		<textarea rows="5" cols="22">
		<%
		Vector vector=list.getList();
		if(vector!=null&&vector.size()>0){
			for(int i=0;i<vector.size();i++){
				out.println(vector.elementAt(i)+"Already online");
			}
		}
		%>
		</textarea><br><br>
		 <a href="loginOut.jsp">Return</a>
		 </td>
	</tr>
</table>
</div>
</body>
</html>

index.jsp page

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" errorPage="" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>Monitor and view online users</title>
</head>
<script language="javascript">
function checkEmpty(form){
for(i=0;i<form.length;i++){
if(form.elements[i].value==""){
alert("Form information cannot be empty");
return false;
}
}
}
</script>
<link href="css/style.css" rel="stylesheet" type="text/css">
<body>
<div align="center">
  <table width="400" height="150" border="0" cellpadding="0" cellspacing="0" bgcolor="lightblue">
  	<Tr><td>&nbsp;</td></Tr>
    <tr>
      <td align="center">
	    <form name="form" method="post" action="LoginList.jsp" onSubmit="return checkEmpty(form)">
    <input type="text" name="user"><br><br>
    <input type="submit" name="Submit" value="Login Online">
  </form>
	  </td>
    </tr>
  </table>
</div>
</body>
</html>

loginOut.jsp page

<%@ page contentType="text/html; charset=gb2312" language="java" errorPage="" %>
<%
         session.invalidate();
 out.println("<script>parent.location.href='index.jsp';</script>");
%>

Tags: Programming Java Session JSP xml

Posted on Sat, 30 Nov 2019 03:42:04 -0800 by hl_tan