Magazine
 
Tips & Tricks
 

if(activeSessions > 0){
activeSessions—;
destroyedSessions++;
}
}
public static int getActiveSessions() {
return activeSessions;
}
public static int getInactiveSessions() {
return destroyedSessions;
}
public static String getActiveSessionID() {
return aSID;
}
public static String getInactiveSessionID() {
return iaSID;
}
}

Now create a servlet that will use above listener’s variables to show the current status of session. ServletSessionCounter.java

ServletSessionCounter.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class ServletSessionCounter extends
HttpServlet{
public void doGet(HttpServletRequest
request, HttpServletResponse response)
throws ServletException, IOException{
response.setContentType(“text/html”);
PrintWriter pw = response.getWriter();
HttpSession session=
request.getSession();
pw.println(“No. of active sessions :” +
SessionCounter.getActiveSessions()+”<br>”);
pw.println(“No. of inactive sessions :” +
SessionCounter.getInactiveSessions()+”<br>”);
pw.println(“Last Activated Session ID :”
+
SessionCounter.getActiveSessionID()+”<br>”);
pw.println(“Last Inactivated Session ID :”
+
SessionCounter.getInactiveSessionID()+”<br>”);
session.setMaxInactiveInterval(2);
}
}

 

Now register the listener in WEB-INF/web.xml
file which makes aware the server about the
SessionListener class that results in calling its
event methods whenever a session is created
and destroyed. Just add the following lines to
the web.xml file.
web.xml:
…………………
…………………
<listener>
<listener-class>SessionCounter</
listener-class>
</listener>
…………………
…………………

Now restart the server and call the servlet many times in different browser windows. The result will be shown like below:



4- Copy a file or directory to the
specified location

This program explains copying a file or directory and all its sub-directories and files to any location.

import java.io.*;
public class CopyDirectory
{
public static void main(String[] args) throws
IOException
{
CopyDirectory cd = new CopyDirectory();
BufferedReader in = new
BufferedReader(new
InputStreamReader(System.in));
System.out.println(“Enter the source

Oct 2007 | Java Jazz Up | 74
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