Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize online user Statistical Analysis based on Session in JSP

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to achieve online user statistical analysis based on Session in JSP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

As a rising star, JSP can occupy a certain position in the server programming environment, which is closely related to its good support for a series of industry standards. Session is one of the infrastructure it provides. As a programmer, you can easily implement simple session-based user management without worrying about how it is implemented on the client side. There are now several different ways to deal with online users.

One is that the page refresh is controlled by the user, and the server controls a timeout such as 30 minutes, after which the user is kicked out without action. The advantage of this method is that if the user forgets to exit, it can prevent others from acting maliciously. The downside is that if you are doing something that takes a lot of time, beyond this time limit, you may have to log in again when you submit. If the original leaf surface is forced to fail again, you may lose your work. From an implementation point of view, this is the simplest, and this is the pattern implemented by default on the server side.

Another way is, the site uses a frame structure, there is a Frame or hidden iframe constantly refreshing, so that you will never be kicked out, but the server side in order to determine whether you are online, you need to set a daze time, if you exceed this daze time in addition to this automatic refresh of the leaf surface outside the refresh other leaves, then you are not online. A typical example of this approach is xici.net. His advantage is that he can use continuous refresh to achieve some server-push-like functions, such as sending messages between netizens.

No matter which mode, some extra work needs to be done in order to browse all the current online users. There is no API for the Session list in servlet API.

What can be used is Listener. The Servlet 2.2 and 2.3 specifications are slightly different here. HttpSessionBindingListener in 2. 2 can implement classes that notify you when the Attribute in a HTTPSession changes. 2. 3 also introduced HttpSessionAttributeListener. Given that I am using Visual age for java 4 and JRun server 3.1, which do not directly support Servlet 2.3 programming, I am using HttpSessionBindingListener here.

What needs to be done is to make a new class to implement the HttpSessionBindingListener interface. There are two methods for this interface:

Public void valueBound (HttpSessionBindingEvent event) public void valueUnbound (HttpSessionBindingEvent event) when you execute Session.addAttribute (String,Object), call your valueBound method if you have added a class that implements the HttpSessionBindingListener interface as Attribute,Session. 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);

}

}

}

}

Assume that the JDBCUser class is an arbitrary User class. When performing a user login, add both the User class and the HttpSessionBinging class to the Session.

In this way, each time the user logs in, a record is added to the vector of attribute "activeSessions" in application. Whenever session times out, valueUnbound is triggered, and delete the session that will be timed out in this vector.

Public void login ()

Throws ACLException,SQLException,IOException

{

/ * get JDBC User Class * /

If (user! = null)

{

Logout ();

}

{

/ / if session time out, or user didn't login

Save the target url temporary.

JDBCUserFactory uf = new JDBCUserFactory ()

If ((this.request.getParameter ("userID") = = null)

| | (this.request.getParameter ("password") = = null))

{

Throw new ACLException ("Please input a valid"

UserName andpassword. ");}

JDBCUser user = (JDBCUser) uf.UserLogin (

This.request.getParameter ("userID"),

This.request.getParameter ("password");

User.touchLoginTime ();

This.session.setAttribute ("user", user);

This.session.setAttribute ("BindingNotify", new

HttpSessionBinding (application))

}

}

When Login, add User and the class for the purpose of BindingNotofy to session. When you logout, you should take the initiative to delete the session from the vector of activeSessions.

Public void logout ()

Throws SQLException,ACLException

{

If (this.user = = null & & this.session.getAttribute ("user") = = null)

{

Return;

}

Vector activeSessions = (Vector) this.application.

GetAttribute ("activeSessions");

If (activeSessions! = null)

{

ActiveSessions.remove (this.session)

Application.setAttribute ("activeSessions", activeSessions);

}

Java.util.Enumeration e = this.session.getAttributeNames ();

While (e.hasMoreElements ())

{

String s = (String) e.nextElement ()

This.session.removeAttribute (s);

}

This.user.touchLogoutTime ();

This.user = null

}

This is the end of the article on "how to implement Session-based online user statistical analysis in JSP". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report