HttpSessionListener is a session listener. It has two methods: public void sessionCreated (HttpSessionEvent event) {}and public void sessionDestroyed (HttpSessionEvent event ){}, the former is executed when the session is created, and the latter is executed when the session is destroyed. By listening to the current session, the number of online users can be counted.
The Code is as follows:
First, create a listener class CountLineListener to implement the HttpSessionListener interface, and add the unimplemented Methods sessionCreated () {} and sessionDestroyed (){}:
[Java]
Package com. test. listener;
Import javax. servlet. ServletContext;
Import javax. servlet. http. HttpSessionEvent;
Import javax. servlet. http. HttpSessionListener;
Public class CountLineListener implements HttpSessionListener {
/***********
* Called when a session is created
*/
Public void sessionCreated (HttpSessionEvent event ){
System. out. println ("create session ......");
ServletContext context = event. getSession (). getServletContext ();
Integer count = (Integer) context. getAttribute ("count ");
If (count = null ){
Count = new Integer (1 );
} Else {
Int co = count. intValue ();
Count = new Integer (co + 1 );
}
System. out. println ("current user count:" + count );
Context. setAttribute ("count", count); // Number of stored persons
}
/************
* Called when session is destroyed
*/
Public void sessionDestroyed (HttpSessionEvent event ){
System. out. println ("Destroy session ......");
ServletContext context = event. getSession (). getServletContext ();
Integer count = (Integer) context. getAttribute ("count ");
Int co = count. intValue ();
Count = new Integer (co-1 );
Context. setAttribute ("count", count );
System. out. println ("current user count:" + count );
}
}
After the listener class is written, configure the listener class in web. xml and add the Code:
[Java]
<Listener>
<Listener-class> com. test. listener. CountLineListener </listener-class>
</Listener>
You can use servlet to write a small example:
(1) login. jsp
[Html]
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> My JSP 'index. jsp 'starting page </title>
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
</Head>
<Body>
<Form method = "POST" action = "<% = request. getContextPath () %>/MyServlet">
<Input type = "text" name = "username"/>
<Br/> <input type = "submit" value = "Logon"/>
</Form>
</Body>
</Html>
Click Log On ----> MyServlet
(2) MyServlet. java
[Java]
Package com. test. servlet;
Import java. io. IOException;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
Public class MyServlet extends HttpServlet {
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
This. doPost (request, response );
}
Public void doPost (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
String user = request. getParameter ("username ");
Request. getSession (). setAttribute ("user", user );
Request. getRequestDispatcher ("/index. jsp"). forward (request, response );
}
}
Log on to the index. jsp page to display the number of online users:
[Html]
<% @ Page language = "java" import = "java. util. *" pageEncoding = "UTF-8" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> My JSP 'index. jsp 'starting page </title>
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "description" content = "This is my page">
</Head>
<Body>
Here is the homepage, the current traffic:
<%
ServletContext context = session. getServletContext ();
Integer count = (Integer) context. getAttribute ("count ");
%>
<% = Count %>
<Br/>
Current user: $ {sessionScope. user}
</Body>
</Html>
In this way, we can easily calculate the number of online users. If you have a "Exit System" link on the page, you can call the session. the invalidate () command is run to clear the session so that the number of online users is-1. If the user does not click "Exit System", but closes the browser directly? I did a test myself. After the browser is closed for a while, the server will automatically execute the sessionDestroyed () method to destroy the session. At this time, the number of users is-1, but sometimes the session will not be executed, I don't know why. Please study it slowly ......