In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.