Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use the HttpSessionListener listener

Shulou Source: shulou.com Published: 2022-06-01 05:12:18 11月14日 Update

Most people do not understand the knowledge points of this article "how to use HttpSessionListener listeners", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "how to use HttpSessionListener listeners" article.

HttpSessionListener listeners define listeners package lee; import javax.servlet.*;import javax.servlet.annotation.*;import javax.servlet.http.*;import java.util.*;@WebListenerpublic class OnlineListener implements HttpSessionListener {/ / trigger the method public void sessionCreated (HttpSessionEvent se) {HttpSession session = se.getSession (); ServletContext application = session.getServletContext () when the session starts between the user and the server / / get session ID String sessionId = session.getId (); / / if it is a new session if (session.isNew ()) {String user = (String) session.getAttribute ("user"); / / unlogged-in users deal with user = (user = = null) as tourists? "tourists": user; Map online = (Map) application.getAttribute ("online"); if (online = = null) {online = new Hashtable ();} / / put user online information into Map online.put (sessionId, user); application.setAttribute ("online", online) }} / / trigger the method public void sessionDestroyed (HttpSessionEvent se) {HttpSession session = se.getSession (); ServletContext application = session.getServletContext (); String sessionId = session.getId (); Map online = (Map) application.getAttribute ("online") when the session is disconnected between the user and the server If (online! = null) {/ / delete the user's online information online.remove (sessionId);} application.setAttribute ("online", online);}} Test JSP user online information online user: test results

Application scenario of HttpSessionListener listener

Application scenario: used to count the current number of people online

Implement HttpSessionListenerimport javax.servlet.ServletContext;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;public class MyHttpSessionListener implements HttpSessionListener {@ Override public void sessionCreated (HttpSessionEvent httpSessionEvent) {System.out.println ("httpsession created");} @ Override public void sessionDestroyed (HttpSessionEvent httpSessionEvent) {System.out.println ("httpsession destroyed") }} login interface to create HttpSessionListenter $Title$ logout to destroy HttpSessionListenter $Title$ has exited to count the number of logins (multithreaded concurrency)

Configure snooping in web.xml

The number of people counted by MyHttpSessionListener myServletContextListener is indeed the largest in the domain of ServletContextListener.

Because HttpSessionListener listeners are only valid in the current session

(1) create a ServletContextListener listener and set the initial value to 0

Import javax.servlet.ServletContext;import javax.servlet.ServletContextEvent;import javax.servlet.ServletContextListener;public class myServletContextListener implements ServletContextListener {@ Override public void contextInitialized (ServletContextEvent servletContextEvent) {ServletContext sc = servletContextEvent.getServletContext (); sc.setAttribute ("count", 0);} @ Override public void contextDestroyed (ServletContextEvent servletContextEvent) {}}

2) change the number of people online

Import javax.servlet.ServletContext;import javax.servlet.http.HttpSession;import javax.servlet.http.HttpSessionEvent;import javax.servlet.http.HttpSessionListener;public class MyHttpSessionListener implements HttpSessionListener {@ Override public void sessionCreated (HttpSessionEvent httpSessionEvent) {System.out.println ("httpsession created"); countPersion (httpSessionEvent.getSession () .getServletContext (), true);} @ Override public void sessionDestroyed (HttpSessionEvent httpSessionEvent) {System.out.println ("httpsession destroyed") CountPersion (httpSessionEvent.getSession (). GetServletContext (), false);} / * * number of changes online * * / public void countPersion (ServletContext sc, boolean isAdd) {/ / to prevent multithreaded concurrency, lock synchronized (sc) {/ / get the current number of online Integer count = (Integer) sc.getAttribute ("count") If (isAdd) {sc.setAttribute ("count", + + count);} else {sc.setAttribute ("count",-- count);}

(3) go to the front page to get the display.

$Title$ Welcome to the current online population ${count} exit the above is about "how to use the HttpSessionListener listener" this article, I believe you all have a certain understanding, I hope the content shared by the editor will be helpful to you, if you want to know more about the relevant knowledge, please follow the industry information channel.

Tags: Monitoring online listeners number of people users content information login statistics between scenarios articles methods servers visitors knowledge articles threads applications services Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Huawei Docker OPPO Reno MySQL