Online statistics on the implementation of the Internet to find the next knowledge of this area, initially my idea is that the management session, if the session destroyed on the reduction, if the landing of the user will add a, but if the user illegally quit, such as: Not logged off, browser, etc. This user's session is not managed, and finally decided to use the Httpsessionlistener interface or Httpsessionbindinglistener interface to achieve, by listening to the session of the new and destroyed to control, detailed below.
First add the landing page index.jsp?
1 2 3 4 5 6 7 8 9 10 11 12-13 |
<%@ page contenttype= "Text/html;charset=utf-8"%> < HTML > < head > < title >test</title > |
Click on the login.jsp to jump after landing (in order to facilitate the use of JSP to do the servlet, the students remember to change over when you use it)?
1 2 3 4 5 6 7 8 9 (a) |
<%@ page contenttype= "text/html;charset=utf-8"%> <%@ page import= "java.util.*"%> <% Request.setcharacterencoding ("UTF-8"); //Get login username String username = request.getparameter ("username"); //Save user name in session Session.setattribute ("username", username); //Put the username in the online list list onlineuserlist = (list) Application.getattribute ( "Onlineuserlist"); //Before first use, you need to initialize if (onlineuserlist = null) { onlineuserlist = new ArrayList (); Application.setattribute ("Onlineuserlist", onlineuserlist); } Onlineuserlist.add (username); //Success Response.sendredirect ("result.jsp"); %> |
Login successful jump to display page result.jsp?
1 2 3 |
<%@ page contenttype= "text/html;charset=utf-8"%> <%@ page iselignored= "false"%> <% @page import= " Java.util.List "%> |
?
1 2 3 4, 5 6 7 8 9 10 11 12 13 14 15 |
< h3 > Hello: ${username} [< a href= "logout.jsp" > Logoff </a >] |
Click the logout page logout.jsp page?
1 2 3 4 5 6 7 8 9 10 11 12-13 |
<%@ page contenttype= "text/html;charset=utf-8"%> <%@ page import= "java.util.*"%> <%//Get logged in User name S Tring username = (String) session.getattribute ("username"); Destroy session session.invalidate (); Remove username List onlineuserlist = (list) application.getattribute ("Onlineuserlist") from the online list; Onlineuserlist.remove (username); Successful Response.sendredirect ("index.jsp"); %> |
OK, login, view, logout pages are available, the following start a new listener
1, Httpsessionlistener
Add Class Onlineuserlistener, there are two methods in the Inheritance Httpsessionlistener,httpsessionlistener sessioncreated (httpsessionevent event) and sessiondestroyed (Httpsessionevent event), the former is listening session of the new, the latter is listening to the destruction of the session.
The Onlineuserlistener code is as follows:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 The 25 26 |
Package com.test; import java.util.List; Import Javax.servlet.ServletContext; Import javax.servlet.http.HttpSession; Import javax.servlet.http.HttpSessionEvent; Import Javax.servlet.http.HttpSessionListener; /** * @author version * * public class Onlineuserlistener implements Httpsessionlistener { &nbs p; public void sessioncreated (Httpsessionevent event) { SYSTEM.OUT.PRINTLN ("New Session:" +event.getsession (). GetId ()); } public void sessiondestroyed (Httpsessionevent event) { HttpSession session = Event.getsession (); ServletContext application = Session.getservletcontext (); //Get user name of login String username = (string) session.getattribute ("username"); //Remove user name from online list List onlineuserlist = (list) application.getattribute ("Onlineuserlist"); Onlineuserlist.remove (username); System.out.println (username+) has exited. " ); }} |
Web.xml configuration:?
1 2 3 |
< listener > < Listener-class >com.test.onlineuserlistener</ |