About
Bookstore
RSS
Email
Followus
Becomeafan
JavaExamples
LearnJavaProgrammingbyExamples
Home
1.JavaSEAPI
Servlet
2.JavaEEAPI
3.Frameworks
4.OtherLibraries
5.DesignPatterns
6.IDE/Tools
Searchthissite...
January21,20143:27pm
Youarehere:Home
HowdoIcountnumberofonlineusers?
HowdoIcountnumberofonlineusers?
PostedbyWayanSaryada onMarch16,2006
Whenyouhaveawebapplicationyoumightwanttoknowhowmanyusersarecurrentlyonlineorconnectedtoyourwebsite.Ifyouhave visitedsomeofwebonlineforumsyoucanseeusuallyonthefirstpagethelistoftheironlineusersormaybejustthenumberof currentlyonlineusers. Howdoweknow/counthowmanysessionsorusersarecurrentlyconnectedtoourwebsite.Doyoucaretoknow?Letsseewhats JavaServletAPIoffersusonthismatter. ServletAPIhasaninterfacejavax.servlet.http.HttpSessionListener,animplementationofthisinterfacewillhavetheabilityto benotifiedbytheservletengineatanytimewhenanewsessioniscreatedordestroyed. ThisinterfacehastwomethodstobeimplementedthesemethodsaresessionCreated(HttpSessionEventse)and
sessionDestroyed(HttpSessionEventse).Thesemethodwillbecalledasanotificationthatanewsessionwascreatedandthe
sessionwasabouttobedestroyedrespectively. Nowletscreateoursessionlistener.Thecodebelowiswhatourclassisgoingtobeimplemented. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 package org.kodejava.example.servlet; importjavax.servlet.http.HttpSessionEvent; importjavax.servlet.http.HttpSessionListener; importjavax.servlet.http.HttpSession; importjava.util.List; importjava.util.ArrayList; public class SessionCounter implements HttpSessionListener { private Listsessions = new ArrayList(); public SessionCounter() { } public void sessionCreated(HttpSessionEventevent) { HttpSessionsession = event.getSession(); sessions.add(session.getId()); session.setAttribute("counter", this); } public void sessionDestroyed(HttpSessionEventevent) { HttpSessionsession = event.getSession(); sessions.remove(session.getId()); session.setAttribute("counter", this); }
Facebook social plugin
Find us on Facebook
Learn by Examples
Like
119 people like Learn by Examples.
public int getActiveSessionNumber() { return sessions.size(); } }
TodisplayinformationofcurrentonlineusersweneedtocreateasimpleJSPpage.ThisJSPfilewillgetthenumberofonlineuser fromHttpSessionattributenamedcounterthatwesetinourlistenerabove. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <%@ page import="org.kodejava.example.servlet.SessionCounter" %> <html> <head> <title>Session Counter</title> </head> <body> <% SessionCounter counter = (SessionCounter) session .getAttribute("counter"); %> Numberofonlineuser(s): <%= counter.getActiveSessionNumber() %> </body> </html>
Thefinalsteptomakethelistenerworkingistoregisteritintheweb.xmlfile.Belowistheexamplehowtoregisterthelistenerin
web.xml.
1 <?xmlversion="1.0" encoding="UTF8"?> 2 <!DOCTYPEwebapp 3 PUBLIC "//SunMicrosystems,Inc.//DTDWebApplication2.3//EN" 4 "http://java.sun.com/dtd/webapp_2_3.dtd"> Generated with www.html-to-pdf.net 5 6 <webapp>
Page 1 / 3
1 2 3 4 5 6 7 8 9 10 11 12
<?xmlversion="1.0" encoding="UTF8"?> <!DOCTYPEwebapp PUBLIC "//SunMicrosystems,Inc.//DTDWebApplication2.3//EN" "http://java.sun.com/dtd/webapp_2_3.dtd"> <webapp> <listener> <listener class> org.kodejava.example.servlet.SessionCounter </listener class> </listener> </webapp>
RelatedPosts
HowdoIcapturesessioncreationandremovalevents? HowdoIinvalidateuserssession? HowdoIshareobjectordatabetweenusersinweb application? HowdoIgetanotificationwhensessionattributewas changed? HowdoIknowsessionlastaccesstime? HowdoIcreateahitcounterservlet? HowdoIsendacookieinServlet?
Filedin:Servlet
AboutWayanSaryada
Iamaprogrammer,arunner,anopenwaterdiverandcurrentlylivingintheislandofBali,Indonesia. ViewallpostsbyWayanSaryada
OneResponseto"HowdoIcountnumberofonlineusers?"
al
August23,2013at3:40am JustremembertodothesessionListstatic Reply
WorkfromHome
RecentProjects SimpleJavaProgrammingofWhitepaperScript /Algorithm(Image)...
LeaveaReply
Name(Required) Mail(willnotbepublished)(Required) Website
$30250
View
IndeedAPIintegrationandsearchbarfix(Java, PHPandWordpress)...
30250
View
TwitterforCAD cad/cam,datamining,java,mysql,naturallang...
$250750
Findmoreprojects
View
PostaProject
Poweredby
SubmitComment
Pages
About Bookstore
RecentPosts
HowdoIcreateasimplemail clientprograminSwing? Whatarethesystem propertiesusedforsending email? HowdoIsendanHTML email?
RecentComments
PierreonHowdoIsendemail usingGmailviaTLS? PreethionHowdoImove focusfromJTextAreausing TABkey? AmitonHowdoIgetoperating systemtemporarydirectory/ folder? SrinonHowtomonitorfileor directorychanges?
JavaResources
JavaLanguageSpecification JavaSETechnicalDocumentation JavaTutorials JavaEETechnicalDocumentation JavaEE7Tutorials
Generated with www.html-to-pdf.net
HowdoIreadlastncharacters fromafile?
Page 2 / 3
AffiliateLinks
HowdoIsendanHTML email? HowdoIreadlastncharacters fromafile? JavaSEInstallationTutorial
AmitonHowdoIgetoperating systemtemporarydirectory/ folder? SrinonHowtomonitorfileor directorychanges? SrinonHowtomonitorfileor directorychanges?
JavaEE7Tutorials
AffiliateLinks
ShopTechTitlesonoreilly.com 50%offhostingplansfromGoDaddy!
2014JavaExamples.Allrightsreserved.
Generated with www.html-to-pdf.net
Page 3 / 3