How do I count number of online users?
When you have a web application you might want to know how many users are currently online or connected to your website. If you have visited some of web online forums you can see; usually on the first page; the list of their online users or maybe just the number of currently online users.
How do we know / count how many sessions or users are currently connected to our website. Do you care to know? Let’s see what’s Java Servlet API offers us on this matter.
Servlet API has an interface javax.servlet.http.HttpSessionListener
, an implementation of this interface will have the ability to be notified by the servlet engine at anytime when a new session is created or destroyed.
This interface has two methods to be implemented; these methods are sessionCreated(HttpSessionEvent se)
andsessionDestroyed(HttpSessionEvent se)
. These method will be called as a notification that a new session was created and the session was about to be destroyed respectively.
Now let’s create our session listener. The code below is what our class is going to be implemented.
To display information of current online users we need to create a simple JSP page. This JSP file will get the number of online user from HttpSession
attribute named counter
that we set in our listener above.
The final step to make the listener working is to register it in the web.xml
file. Below is the example how to register the listener inweb.xml.
'Development > JSP & Servlet' 카테고리의 다른 글
jsp - 세션 타임 아웃 설정 (0) | 2012.08.22 |
---|---|
jsp - session 생성 및 삭제 시점 체크하여 처리하기 (0) | 2012.08.22 |
jsp - session 타임아웃 시 페이지 이동 (0) | 2012.08.21 |
jsp - 클라이언트 IP 확인 및 로컬 IP 얻기 (0) | 2012.08.21 |
JSP - 세션 관리 (0) | 2012.07.07 |