How do I capture the session event in the servlet?

Source: Internet
Author: User
Tags exit continue empty log log log

Capture the meaning of the session event:

1, record the site's customer login log (login, exit information, etc.)

2, Statistics online number

3, and so on there are a lot of, oh, I think it ... Anyway, it's very important.

Session represents the client's conversation process, and when the customer logs in, an object is passed in to the session to track the client's sessions. In the servlet, the object passing in the session is an object that implements the Httpsessionbindinglistener interface (for convenience, this object is called a listener), When it is passed in (that is, when the HttpSession object's SetAttribute method is invoked) and when it is removed (that is, when the RemoveAttribute method of the HttpSession object is invoked or session time Out time) The session object automatically invokes the Valuebound and Valueunbound methods of the listener (this is the method in the Httpsessionbindinglistener interface).

So, log in is not difficult to achieve.

Another problem is how to count the number of online people, the problem is a little different from the implementation log log, the number of online statistics (and its information), is to count how many session instances exist now, we can add a counter (if you want to store more information, you can use an object to do the counter, Then given in the example, simple, with an integer variable as a counter, by the Valuebound method in the Counter add 1,valueunbound method in the counter minus 1, you can achieve the number of online statistics. Of course, this will take advantage of the global nature of the ServletContext. (Refer to the servlet specification for a description of ServletContext), create a new listener, and deposit its instance into ServletContext properties to ensure uniqueness of this listener instance, when the client logs on, First determine whether the ServletContext property is empty, if not NULL, the proof has been created, directly put this property into the session, the Counter plus 1, if empty, create a new listener, and into the ServletContext properties.

An example is provided:

Implement a listener:

Sessionlistener.java
Import java.io.*;
Import java.util.*;
Import javax.servlet.http.*;
Monitoring the entire process of logging in
public class Sessionlistener implements Httpsessionbindinglistener
{
Public String privateinfo= ""; Generate initialization parameter string for listener
Private String logstring= ""; Log record string
private int count=0; Number of logged-in counters
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= "\ LOGIN:" +privateinfo+ "Time:" +calendar.gettime () + "\ n";
for (int i=1;i<1000;i++) {
File File=new file ("Yeeyoo.log" +i);
if (! (File.exists ()))
File.createnewfile (); If the file does not exist, create the 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 Create file in Append mode
Foo.write (Logstring.getbytes (), 0,logstring.length ()); Write Log string
Foo.close ();
break;//exit
}
}catch (FileNotFoundException e) {}
catch (IOException e) {}
}
public void Valueunbound (Httpsessionbindingevent event)
{
count--;
if (Privateinfo.equals ("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=new file ("Yeeyoo.log" +i);
if (! (File.exists ()))
File.createnewfile (); If the file does not exist, create the 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 Create file in Append mode
Foo.write (Logstring.getbytes (), 0,logstring.length ()); Write Log string
Foo.close ();
break;//exit
}
}catch (FileNotFoundException e) {}
catch (IOException e) {}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.