Original address: http://blog.csdn.net/jiaoxueli/article/details/2226134
Considering the number of online users in the project and the requirement that the same user can only log in once, inquire about the use of Httpsessionbindinglistener interface, record for later use, also for the same needs of colleagues reference.
Here is my test example, first build a Web project, examples of programs include: Onlineuser.java, login.jsp, logout.jsp,onlineuser.jsp four files
Onlineuser.java list:
Package pub;
/*
* Onlineuser class implements Httpsessionbindinglistener interface
* The Onlineuser class will have unique properties for the Httpsessionbindinglistener interface
* So what are the unique properties of the Httpsessionbindinglistener interface?
* The Httpsessionbindinglistener interface has two empty functions
* public void Valuebound (Httpsessionbindingevent e)
* public void Valueunbound (Httpsessionbindingevent e)
*
* Onlineuser Implement some methods: Get the number of online users
*
*/
Import javax.servlet.http.*;
Import java.util.*;
public class Onlineuser implements Httpsessionbindinglistener{
Public Onlineuser (){
}
Private vector users=new vector ();
public int GetCount (){
Users.trimtosize ();//Adjust the size of the vector users to the vector users
return users.capacity ();//Returns the capacity of 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 ("Request:::::::::.:" +e.getname ());
if (!existuser (E.getname ())){
Users.add (E.getname ());
System.out.print (E.getname () + "Log in to System" + (New Date ()));
System.out.println ("Number of online users:" +getcount ());
}else{
System.out.println (E.getname () + "already exists");
}
}
public void Valueunbound (Httpsessionbindingevent e) {
Users.trimtosize ();
String Username=e.getname ();
DeleteUser (UserName);
System.out.print (username+ "Exit system" + (New Date ()));
System.out.println ("Number of online users:" +getcount ());
}
}
login.jsp list:
<%@ Page contenttype= "text/html;charset=gb2312"%>
<title> Test Httpsessionbindinglistener Interface </title>
<body>
<form name= "Login" method= "POST" action= "onlineuser.jsp" >
<input type=text name= "username" >
<input type=submit name= "Submit" value= "Login" >
</form>
</body>
logout.jsp list:
<%@ Page contenttype= "text/html;charset=gb2312"%>
<%@ Page import= "pub. Onlineuser,java.util.* "%>
<jsp:usebean id= "Onlineuser" class= "pub. Onlineuser "scope=" Application "/>
<title> get rid of JSP online number </title>
<body>
<center>
</center>
<%&NBSP;
string username=request.getparameter ("username") );
if (Username!=null&&onlineuser.deleteuser (username)) {
out.println (username+ "has exited the system!");
session.removeattribute (username);
out.println ("<script>window.location=" login.jsp "; </ Script> ");
}else{
out.println (username+ "has exited the system!) ");
out.println ("<script>window.location=" login.jsp "; </ Script> ");
}
%>
<center>
<p>elapsed production </p>
<p> </p>
<p><a href= "logout.jsp" > Exit system </a></p>
</ center>
</body>
onlineuser.jsp List
<%@ Page contenttype= "text/html;charset=gb2312"%>
<%@page import= "java.util.*,pub.*"%>
<jsp:usebean id= "Onlineuser" class= "pub. Onlineuser "scope=" Application "/>
<title> get rid of JSP online number </title>
<body>
<center>
</center>
<%Session = Request.getsession (false); %>
<%
String Username=request.getparameter ("username");
if (Onlineuser.existuser (username)) {
Out.println ("User <font color=red>" +username+ "</font> has landed! ");
}else{
Session.setmaxinactiveinterval (+);//sesion effective duration, in seconds
Session.setattribute (Username,onlineuser);
OUT.PRINTLN ("Welcome new User: <font color=red>" +username+ "</font> login to the system! ");
}
Out.println ("<br> Current number of online users: <font color=red>" +onlineuser.getcount () + "</font><br>");
Vector Vt=onlineuser.getonlineuser ();
Enumeration E = Vt.elements ();
Out.println ("Online user list");
Out.println ("<table border=1>");
Out.println ("<tr><td> User name </td></tr>");
/*while (E.hasmoreelements ()) {
Out.println ("<tr><td>");
Out.println (String) e.nextelement () + "<br>");
Out.println ("</td></tr>");
}
The following method is also possible, both methods are feasible, in fact, iterator is the enumeration subclass
*/
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 Production </p>
<p> </p>
<%
Out.println ("<p><a href= ' logout.jsp?username=" +username+ "' > Exit System </a></p>");
%>
</center>
</body>
The listener does not need to configure listener in Web. XML, completes the deployment launch, and accesses the login.jsp test.
about using Httpsessionbindinglistener to get the number of online users, the same user logged in once