بادئ ذي بدء ، هذه الإحصائيات لها عيب في عدد الأشخاص عبر الإنترنت. يمكن أن يكون لدى شخص جلسات متعددة في نفس الوقت ، مما يؤدي إلى بعض الإحصاءات غير الدقيقة.
بعد ذلك ، ابدأ في كتابة الرمز.
الخطوة 1: تنفيذ الطريقة في httpsessionlistener ، أضف التعليق التوضيحي @weblistener
webListener SessionLister Public Class Practirements httpsessionListener {public void sessioncreated (httpsessionevent arg0) {// todo method method method method servletcontext context = arg0.getsession (). if (context.getAttribute ("count") == null) {context.setAttribute ("count" ، 0) ؛ } آخر {int count = (integer) context.getAttribute ("count") ؛ context.setAttribute ("count" ، count+1) ؛ }} public void sessiondestroyed (httpsessionevent arg0) {// todo method method method clud servletcontext context = arg0.getSession (). getServletContext () ؛ if (context.getAttribute ("count") == null) {context.setAttribute ("count" ، 0) ؛ } آخر {int count = (integer) context.getAttribute ("count") ؛ if (count <1) {count = 1 ؛ } context.setAttribute ("count" ، count-1) ؛ } جلسة httpsession = arg0.getSession () ؛ اسم السلسلة = (سلسلة) session.getAttribute ("name") ؛ hashset <string> nameset = (hashset <string>) context.getAttribute ("nameset") ؛ nameset.remove (الاسم) ؛ }}الخطوة 2: التحكم في إنشاء الجلسة ووضعها في الكائن
جلسة httpsession = request.getSession () ؛ session.setAttribute ("الاسم" ، الاسم) ؛ عدد الكائن = context.getAttribute ("count") ؛ if (count == null) {count = 0 ؛ }الخطوة 3: ابدأ الفصل مع التعليق التوضيحي servletcomponentscan ، حتى تتمكن من المسح الضوئي إلى المستمع
ملاحظة ، هذا الرمز مناسب لتطوير Boot Spring-Boot
ببساطة ، أضف مستمعًا في javaweb إلى web.xml
<StaNeer> <stanker-class> zjq.listener.sessionListener </beader-class> </stanker>
لخص
ما ورد أعلاه هو رمز التنفيذ لـ Spring Boot لحساب عدد المستخدمين عبر الإنترنت من خلال HTTPSessionListener Monitor الذي قدمه لك المحرر. آمل أن يكون ذلك مفيدًا للجميع. إذا كان لديك أي أسئلة ، فيرجى ترك رسالة لي وسوف يرد المحرر على الجميع في الوقت المناسب. شكرا جزيلا لدعمكم لموقع wulin.com!