Magazine
 
Tips & Tricks
 

The above program creates “MyPDF.pdf” file
as in the figure below:



 

3- Count number of active users through servlet

A session is just like a temporary unique connection between the client (browser) and server which helps the server to keep track of the specific user. Session creation and destruction events can help to count the number of active session. We can create listener object
that will be called every time a session is created or destroyed by the server.

In this section, SessionCounter is a listener class which will be registered in the web.xml file. Create SessionCounter class and implement HttpSessionListener interface. Implement its two methods sessionCreated() and sessionDestroyed() passing HttpSessionEvent as an argument. In the first method sessionCreated(), increase the value of the variable responsible for counting the number of active sessions because one new session has been created and in the second method sessionDestroyed () decrease the value of the same by one because one session has been ended. Save this file to the WEB-INF/classes folder of Tomcat server.


SessionCounter.java

import
javax.servlet.http.HttpSessionListener;
import javax.servlet.http.HttpSessionEvent;
public class SessionCounter implements
HttpSessionListener {
private static int activeSessions = 0;
private static int destroyedSessions = 0;
private static String aSID = null;
private static String iaSID = null;
public void sessionCreated(HttpSessionEvent se) {
aSID = se.getSession().getId();
activeSessions++;
}
public void
sessionDestroyed(HttpSessionEvent se) {
iaSID = se.getSession().getId();

Oct 2007 | Java Jazz Up | 73
previous
index
next
 
Pages: 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, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78,   Download PDF