JSP as an Up-and-comer can occupy a certain position in the server programming environment, and it is closely related to the good support of a series of industry standards. Session is one of the infrastructure that it provides. As a programmer, you can easily implement simple session-based user management by not mind how the client is implemented. There are now several different ways to deal with online users.
One is the page refresh by the user control, server-side control of a timeout such as 30 minutes, to the time after the user did not move to be kicked out. The advantage of this method is that if the user forgets to quit, it can prevent others from malicious action. The downside is that if you're doing a time consuming thing, exceeding the time limit, you may have to face the landing again when you submit. If the original leaf is forced to fail, it is possible to lose the work you do. In the perspective of implementation, this is the simplest, the server side is the default implementation of this pattern.
Another way is, the site uses a frame structure, there is a frame or hidden iframe constantly refreshed, so you will never be kicked out, but the server to determine whether you are online, you need to set a daze time, If more than this daze time you in addition to this automatic refresh of the page does not refresh other pages, I think you are no longer online. The typical way to take this approach is xici.net. His advantage is that you can use the constant refresh to implement some similar server-push functions, such as sending messages between netizens.
Regardless of the pattern, additional work is required to enable browsing of all current online users. The Servlet API does not have an API for the session list.
What can be used is the listener. The Servlet 2.2 and 2.3 specifications are slightly different here. The Httpsessionbindinglistener in 2.2 enables you to notify your class when the attribute changes in a httpsession. And 2.3 also introduced httpsessionattributelistener. Since I am using visual age for Java 4 and JRun Server 3.1, they do not directly support the programming of Servlet 2.3, I'm using Httpsessionbindinglistener here.
What needs to be done involves doing a new class to implement the Httpsessionbindinglistener interface. This interface has two methods:
public void valueBound(HttpSessionBindingEvent event)
public void valueUnbound(HttpSessionBindingEvent event)
When you perform Session.addattribute (string,object), if you have added a class that implements the Httpsessionbindinglistener interface to Attribute,session will notify you of the class , call your Valuebound method. Instead, the Session.removeattribute method corresponds to the Valueundound method.
public class Httpsessionbinding implements Javax.servlet.http.HttpSessionBindingListener
{
ServletContext application = null;
Public httpsessionbinding (ServletContext application)
{
Super ();
if (Application ==null)
throw new IllegalArgumentException ("Null application is not accept.");
this.application = Application;
}
public void Valuebound (Javax.servlet.http.HttpSessionBindingEvent e)
{
Vector activesessions = (vector) application.getattribute ("Activesessions");
if (activesessions = null)
{
Activesessions = new Vector ();
}
Jdbcuser Sessionuser = (jdbcuser) e.getsession (). getattribute ("user");
if (Sessionuser!= null)
{
Activesessions.add (E.getsession ());
}
Application.setattribute ("Activesessions", activesessions);
}
public void Valueunbound (Javax.servlet.http.HttpSessionBindingEvent e)
{
Jdbcuser Sessionuser = (jdbcuser) e.getsession (). getattribute ("user");
if (Sessionuser = null)
{
Vector activesessions = (vector) application.getattribute ("Activesessions");
if (activesessions!= null)
{
Activesessions.remove (E.getsession (). GetId ());
Application.setattribute ("Activesessions", activesessions);
}
}
}
}