HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"Servlet Session Display"
Volume: 5 Issue: 2, p.12

	

Listing 1: 

<HTML> 
<TITLE>Session Unit Tester</TITLE> 
<H1>Session Unit Tester</H1> 

<FONT COLOR="Teal">Although caching is 
 turned off on this page, it is safer 
 to do a manual refresh to be sure the 
 most current 
 session is examined.</FONT> 

<% 
   //Turn off all browser caching 
   response.setHeader("pragma","no-  cache"); 
   response.setHeader("cache control","no"); 
   response.setHeader("expires","0"); 
  
   HttpSession session = 
      request.getSession(false); 
   if (session != null) 
   { 
      boolean fDeep = 
         request.getParameter("De- 
         tails") != null; 
      String buttonName = fDeep ? 
           "VALUE=\"Hide Details\"" : 
           "NAME=\"Details\" "+ 
              "VALUE=\"Show Details\""; 
%> 
<FORM METHOD="GET"> 
   <INPUT TYPE="SUBMIT" <%= buttonName%> > 
</FORM> 
<% 
      SessionTesterBeans.SessionTesterBean. 
         dumpSession( session, out, fDeep ); 
   } 
   else 
   { 
%> 

<H3>Couldn't get a session :-(</H3> 

Suggestions for getting a session:<OL> 
<LI>Hit the refresh button. 
<LI>Make sure you have a session in 
 your application; it may have expired. 
 Try to start a new session in your 
 application. 
<LI>Check the application's session 
 configuration. 
</OL> 

<% 
   } // end else 
%> 

</HTML> 
  

Listing 2: 
  
package SessionTesterBeans; 

import java.beans.*; 
import java.util.*; 
import java.text.*; 
import java.io.*; 
import java.lang.reflect.*; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import java.beans.*; 
import java.util.Date; 

/** 
* A set of static methods to test and 
* dump servlet sessions. Normally to be 
* used from SessionTester.jsp, but can 
* also be used from the command line 
* (see main()) or by calling these 
* methods directly. 
* <P> 
* All dumping methods accept an object 
* and an output stream. 
* The display of the object is created 
* as HTML and written to the stream. 
*/ 
public class SessionTesterBean { 

// don't display property values longer 
// than this 
static final int MAX_DISPLAYABLE_STRING = 100; 

// time format used by epochTimeToString() 
static final SimpleDateFormat myDateFormat = 
   new SimpleDateFormat( "HH:mm:ss.SSS 
                       'on' "+ 
                       "MM/dd/yyyy" ); 

/** 
* Diplays the content of a session, 
* including indentification of 
* each object stored in the session. 
* If fDeep is true, each such object 
* is also displayed using dumpSerializ- 
* able() and dumpBean() in this class. 
* @param session Session to be dis- 
* played. 
* @param out The stream to which HTML 

* is written. 
* @param fDeep True to perform a dump 
* of each session object. 
*/ 
public static void 
dumpSession( HttpSession session, 
            PrintWriter out, 
            boolean fDeep ) 
{ 
   if (session == null) { 
      out.println( "<BR>Null session 
      passed to "+ 
         "SessionUnitTester.dumpSes- 
          sion()" ); 
      return; 
   } 

   if (fDeep) { 
      out.println( "<BR>Current  time: " + 
         myDateFormat.format(new Date())); 
      out.println( "<BR>Access   time: " + 
         myDateFormat.format(new Date( 
            session.getCreationTime())) ); 
      out.println( "<BR>Creation time: " + 
         myDateFormat.format(new Date( 
            session.getLastAccessed- 
            Time())) ); 
      out.println( "<BR>session id: " + 
         session.getId() ); 
      out.println( "<BR>timeout: " + 
         session.getMaxInactiveInterval() ); 
      out.println( "<BR>new: " + 
         session.isNew() ); 
   } 

   String[] names = session.getValue- 
   Names(); 
   out.println( "<P>Found "+ 
                names.length + 
                " session 
                objects:<OL>" ); 

   Object sessObj; 
   int    totalObjSize = 0; 

   for (int i = 0; i < names.length; ++i) { 
      out.println( "<P><LI>" + names[i] ); 
      sessObj = session.getValue( names[i] ); 
      if (sessObj == null) { 
         out.println( 
            "<BR><B>Can't get this ses- 
             sion "+ 
            "object.</B>" + 
            " Possibly non-Serializ- 
              able." ); 
      } 
      else { 
         totalObjSize += 
            dumpSerializable( sessObj, 
                             out, 
                             fDeep ); 

         /* Bean dumping code that's 
         /* too long to show in this 
         /* article 
            if (fDeep) { 
               dumpBean( sessObj, out ); 
            } 
         */ 
      } 
   } 
   out.println( 
      "</OL>Total size of session 
       objects = " + 
      totalObjSize + "<BR>" ); 
} 

/** 
* Displays a (supposedly) Serializable 
* object by attempting to write the 
* object to determine if the object is * serializable, and if so, how big 
* the object is. If fDeep is true, dis- 
* plays the name of each field in the 
* class. 
* <P>The purpose of this display is to 
* assist the developer in determining 
* if an object can be serialized in a 
* servlet session, and to report the 
* size and composition of the object 
* so it can be controlled at an early 
* stage in the development process. 
* @param sessObj An object to be dis- 
* played. 
*        Although Serializable objects 
*        are expected, any object can 
*        be used. 
*        Non-serializable objects 
*        (including non-serializable 
*        objects embedded in 
*        sessObj) will be reported as 
*        such. 
* @param out The stream to which HTML 
* is written. 
* @param fDeep True to display the 
* fields of the object. 
* @return Serialized size of sessObj 
* (zero if couldn't be serialized). 
*/ 
public static int 
dumpSerializable( Object sessObj, 
                 PrintWriter out, 
                 boolean fDeep ) 
{ 
   if (sessObj == null) { 
      out.println( "<BR>Null object 
      passed to "+ 
         "SessionUnitTester.dumpSerial- 
          izable()" ); 
      return 0; 
   } 

   Class sessClass = sessObj.get- 
   Class(); 
   out.println( "<BR>Class name = "+ 
                sessClass.getName() 
                +"<BR>" ); 

   int sessObjSize = 0; 

   try { 
      ByteArrayOutputStream baSessStrm = 
         new ByteArrayOutputStream(); 
      ObjectOutputStream objSessStrm = 
         new ObjectOutputStream( 
         baSessStrm ); 
      objSessStrm.writeObject( sessObj ); 
      objSessStrm.close(); 
      sessObjSize = baSessStrm.size(); 
      out.println( "Serialized size = "+ 
                   sessObjSize ); 
   } 
   catch (Exception xcpt) { 
      if (xcpt instanceof 
          NotSerializableException) { 
         out.println( "<B>This object 
                      is not "+ 
                      "serializ- 
                      able.</B><BR>" ); 
      } 
      out.println( "While attempting to "+ 
                  "serialize, this 
                  exception "+ 
                  "occurred:<BR>" ); 
      xcpt.printStackTrace( out ); 
   } 

   if (fDeep && !(sessClass.isInterface() || 
               sessClass.isPrimitive() || 
               sessClass.isArray()       ) ) { 
      Field[] fields = 
         sessClass.getDeclaredFields(); 
      if (fields.length > 0) { 
         out.println( "<P>found "+ 
                      fields.length + 
                      " object 
                      fields:<OL>" ); 
         for (int ifield = 0; 
              ifield < fields.length; 
              ++ifield) { 
            out.println( "<LI>" + 
                fields[ifield].to- 
                String() ); 
         } 
         out.println( "</OL>" ); 
      } 
      else { 
         out.println( 
            "<P>no object fields found" ); 
      } 
   } 

   return sessObjSize; 
} 

/** 
* Stand-alone test for an object. 
* The program argument is the name of a 
* class. 
* If the class can be loaded and an 
* object created, that object is dis- 
* played (with 
* HTML tags) using <B>dumpSerializ- 
* able</B>. 
*/ 
public static void main( String[] args ) 
{ 
   PrintWriter out = 
      new PrintWriter( System.out ); 

   out.println( "Argument is name of 
                class to "+ 
                "test for session 
                properties." ); 
   if (args.length == 1) { 
      final String testClassName = 
      args[0]; 
      try { 
         Class testClass = 
            Class.forName( testClass- 
            Name ); 
         Object testObject = 
            testClass.newInstance(); 
         dumpSerializable( testObject, out, 
                           true ); 
      } 
      catch (Exception xcpt) { 
         out.println( 
            "Couldn't create an 
            instance of "+ 
            testClassName ); 
         xcpt.printStackTrace( out ); 
      } 
   } 
   out.flush(); 
} 

} // end class SessionTesterBean 



 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: [email protected]

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.