How to capture the session event in the servlet

xiaoxiao2021-03-06  90

The meaning of capturing session events:

1. Record the customer login log (login, exit information, etc.)

2, statistics online number

3, etc. There are still a lot, huh, I think ... I am very important.

Session represents a customer's session process, when you log in, you will pass an object to the Session to track the customer's session. In Servlet, the object that is incompanded if it is an object that implements the HttpSessionBindingListener interface (convenient, this object is called a listener), then when it is incorporated (ie, when calling the setattribute method of the httpsession object) and removed The session object will automatically call the listener valueBound and ValueunBound method (this is the method of the HttpSessionBindingListener interface). It can be seen that the login log is not difficult to achieve.

Another problem is how to count online, this problem is a bit different from the login log, statistics online number (and its information), is the statistics now how many session instances exist, we can add a counter (if you want to store more Information, you can use an object to do a counter. In the example, simply play, use an integer variable as a counter, by adding 1, the counter is added to the counter in the valueunbound method, you can implement online Statistics of the number. Of course, this should be used to use the global feature of servletContext. (Refer to the Servlet specification for the narrative of ServletContext), create a new listener and store it into the properties of servletContext to ensure that this listener instance is unique, when the customer logs in, first determined whether the attribute of servletContext is Empty, if not empty, demonstrate that you have created, remove this property into the session, the counter plus 1; if you are empty, create a new listener and store the properties of ServletContext.

for example:

Implement a listener:

// sessionListener.java

Import java.io. *; import java.util. *; import javax.servlet.http. *;

// Monitor the entire process of login public class sessionListener implements httpsessionBindingListener {

Public String PrivateInfo = ""; // Generates the initialization parameter string private string logstring = ""; // logging string private int count = 0; // Login number counter

Public sessionListener (string info) {this.privateInfo = info;}

Public int getCount () {Return Count;}

public void valueBound (HttpSessionBindingEvent event) {count ; if (privateInfo.equals ( "count")) {return;} try {Calendar calendar = new GregorianCalendar (); System.out.println ( "LOGIN:" privateInfo "TIME: " Calendar.gettime ()); logstring =" / nlogin: " privateInfo " Time: " Calendar.gettime () " / n "; for (int i = 1; i <1000; i ) {File File = New file ("Yeeyoo.Log" i); if (! (file.exiss ())) file.createNewFile (); // If the file does not exist, create this file if (file.length ()> 1048576) // If the file is greater than 1M, recreate a file Continue; fileoutputstream foo = new fileoutputstream ("Yeeyoo.log" i, true); // Open Foo.Write (logstring.getbytes (), 0, Logstring.Length ()); // Write log string foo.close (); break; // exit}} catch (filenotfoundexcection e) {} catch (ooException e) {}} public void valuevent (httpsessionBindingEvent evenet) { Count -; IF ("Count")) {Return;} try {Calendar Calendar = New Gregoriancalendar (); System.out.Println ("Logout:" PrivateInfo "Time:" Calendar.gettime )); logstring = "/ nlogout:" privateInfo "Time:" Calendar.gettime () "/ n"; for (int i = 1; i <1000; i ) {file file = ne w File ("Yeeyoo.Log" i); if (! (file.exiss ())) file.createNewFile (); // If the file does not exist, create this file if (file.length ()> 1048576) / / If the file is greater than 1m, recreate a file contractStream foo = new fileoutputstream ("YeeYoo.log" i, true); // Open Foo.write in Append mode (logstring.getbytes (), 0, logstring .length ()); // Write log string foo.close (); break; // exit}} catch (filenotfoundexception e) {} catch (ooException e) {}}}

} The implementation of the login log:

Let's take a look at some of our source code to use this listener in our login servlet:

... httpsession session = req.getations (true); ... // sessionListener sessionListener = new sessionListener ("IP:" req.getRemEaddr ()); // Start a listener session.setttribute for each session process "Listener", sessionListener; // Implants httpsession, which will activate the listener to call the valueBound method, // thus log file. // When the system exits the login, simply call session.removeattribute ("Listener"); you can automatically call the listener's valueUnbound method. Or, this method will also be called when session time out is.

Statistics of the number of logins:

ServletContext session1 = getServletConfig () getServletContext ();. // made ServletContext object instance if ((SessionListener) session1.getAttribute ( "listener1") == null) {SessionListener sessionListener1 = new SessionListener ( "count"); // provided only Once, it is different from the record of the above log files. // That is, when the first client is connected to the server, the global variable is started, // All customers will use the same context. Session1.setttribute ("Listener1", SessionListener1); // Set the monitor object to servletContext, with global validity, //, all customers can get its instance. } session.setttribute ("Listener1", ("Listener1", ("Listener1")); // Remove this global object, and bind this object into a session, // This will prompt the listener call ValueBound, the counter adds one. You can use the following code to get the current number of logins at any time in the program:

(SessionListener) session.getattribute ("Listener1"))). GetCount () getCount () is a method of the listener, that is, the value of the current counter is also the number of logins.

转载请注明原文地址:https://www.9cbs.com/read-106672.html

New Post(0)