Solved

How can i list/show all session variables

Posted on 2006-10-23
5
4,717 Views
Last Modified: 2011-08-18
In ASP i am able to list all session variables and corresponding values.
How can i do this in JSP?

0
Comment
Question by:dayiku
  • 4
5 Comments
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 50 total points
ID: 17790558
does this help:
http://javaalmanac.com/egs/javax.servlet/State.html

   // Get all request-scoped attributes
    java.util.Enumeration enum = req.getAttributeNames();
    for (; enum.hasMoreElements(); ) {
        // Get the name of the attribute
        String name = (String)enum.nextElement();
   
        // Get the value of the attribute
        Object value = req.getAttribute(name);
    }
   
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 17790566
try:

<%
      /*
            Version: 1.1 : 1st Nov 2004
            Author: ryancys
            Get Session Object and values
      */
%>
<%@page import="java.io.*" %>
<%@page import="java.util.*" %>
<%!
      public static Vector chklist = new Vector();
      
      public static String chkopt(String v1, String v2, String rv){
            if (v1.equals(v2)) {
                  return rv;
            } else {
                  return "";
            }            
      }
%>
<%
      //Clear Cache
      if (request.getProtocol().compareTo("HTTP/1.0") == 0) {
            response.setHeader("Pragma", "no-cache");
      } else if (request.getProtocol().compareTo("HTTP/1.1") == 0) {
            response.setHeader("Cache-Control", "no-cache");
      }
      response.setDateHeader("Expires", 0);
      
      //Check to clear Session
      if (request.getMethod().equalsIgnoreCase("POST")) {
            session.invalidate();
      }
      
      
      String sessionFileName = "session.txt";
      File datafile = null;
      BufferedReader in = null;
      
      String line = "";
      String loadtype = (request.getParameter("loadtype")==null?"":request.getParameter("loadtype").toString().trim());
      long i = 0;
      String tmp = "";
      boolean isItemFound = false;
      
      chklist.clear();
      
      if ((!loadtype.equals("")) && (!loadtype.equals("default"))) {
            sessionFileName = loadtype;
      }
      
      datafile = new File(application.getRealPath("/jsp/"+sessionFileName));
      
      //System.out.println(application.getRealPath("/jsp/"+sessionFileName));
      //System.out.println("datafile.exists() = "+datafile.exists());
      
      if (datafile.exists()) {
            in = new BufferedReader(new FileReader(datafile));
            while ((line = in.readLine()) != null) {
                  try {
                        if (!line.trim().equals("")) {
                              chklist.add(line.trim());
                        }
                  } catch (Exception e) { }
            }
      }      
      
      for (int s=0;s < chklist.size();s++) {
            System.out.println("chklist["+s+"] = " + chklist.get(s).toString());
      }
%>
<html>
<head>
<title>Get Session</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<p><font face="Arial, Helvetica, sans-serif" size="2"><b>Session List: <%=loadtype%></b></font></p>
<% if ((loadtype.equals("")) || (loadtype.equals("default"))) { %>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr bgcolor="#CCCCCC">
    <td width="33" height="29"><b></b></td>
    <td width="259" height="29"><b><font face="Arial, Helvetica, sans-serif" size="2">Attribute
      name</font></b></td>
    <td width="50%" height="29"><b><font face="Arial, Helvetica, sans-serif" size="2">Value</font></b></td>
    <td width="143" height="29"><b><font face="Arial, Helvetica, sans-serif" size="2">Type</font></b></td>
  </tr>
  <%
    java.util.Enumeration enum = null;
      try {
            enum = session.getAttributeNames();
      } catch (Exception e) {}
      Object curAttribute = null;
      String attName = null;
      i = 0;
      if (enum != null) {
            while (enum.hasMoreElements())
            {
                  attName = (String)enum.nextElement();
                  curAttribute = session.getAttribute(attName);
                  i++;
  %>
  <tr>
    <td width="33"><font face="Arial, Helvetica, sans-serif" size="2"><%=i%></font></td>
    <td width="259"><font face="Arial, Helvetica, sans-serif" size="2"><%=attName%></font></td>
    <td width="50%"><font face="Arial, Helvetica, sans-serif" size="2"><%=curAttribute%></font></td>
    <td width="143"><font face="Arial, Helvetica, sans-serif" size="2"><%=curAttribute.toString().length()+";"+curAttribute.getClass()%></font></td>
  </tr>
  <%       }
        }      
      %>
</table>
<% } else { %>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr bgcolor="#CCCCCC">
    <td width="33" height="29"><b></b></td>
    <td width="259" height="29"><b><font face="Arial, Helvetica, sans-serif" size="2">Attribute
      name</font></b></td>
    <td width="50%" height="29"><b><font face="Arial, Helvetica, sans-serif" size="2">Value</font></b></td>
    <td width="143" height="29"><b><font face="Arial, Helvetica, sans-serif" size="2">Type</font></b></td>
  </tr>
  <%
    i = 0;
            
      for (int n=0;n < chklist.size();n++) {
            
            System.out.println("chklist["+n+"] = " + chklist.get(n).toString());
            
            isItemFound = false;
            java.util.Enumeration enum = null;
            try {
                  enum = session.getAttributeNames();
            } catch (Exception e) {}
            Object curAttribute = null;
            String attName = null;
            if (enum != null) {
                  while (enum.hasMoreElements())
                  {
                        attName = (String)enum.nextElement();
                        if (attName.equals(chklist.get(n).toString())) {
                              curAttribute = session.getAttribute(attName);
                              i++;
  %>
  <tr>
    <td width="33"><font face="Arial, Helvetica, sans-serif" size="2"><%=n+1%></font></td>
    <td width="259"><font face="Arial, Helvetica, sans-serif" size="2"><%=attName%></font></td>
    <td width="50%"><font face="Arial, Helvetica, sans-serif" size="2"><%=curAttribute%></font></td>
    <td width="143"><font face="Arial, Helvetica, sans-serif" size="2"><%=curAttribute.toString().length()+";"+curAttribute.getClass()%></font></td>
  </tr>
  <%                        isItemFound = true;            
                                break;      
                          }      
                    }
              }
            if (isItemFound == false) {
                  if (chklist.get(n).toString().equals("")) {
                        tmp = "";
                  } else {
                        tmp = String.valueOf(n+1);
                  }
            %>
        <tr>
            <td width="33"><font face="Arial, Helvetica, sans-serif" size="2"><%=tmp%></font></td>
            <td width="259"><font face="Arial, Helvetica, sans-serif" size="2"><%=chklist.get(n).toString()%></font></td>
            <td width="50%"><font face="Arial, Helvetica, sans-serif" size="2">&nbsp;</font></td>
            <td width="143"><font face="Arial, Helvetica, sans-serif" size="2">&nbsp;</font></td>
        </tr>
        <%
            }
             
        }      
      %>
</table>
<% } %>
<p><font face="Arial, Helvetica, sans-serif" size="2"><b>Total Session Object
  Count: <%=i%></b></font> <br>
  <br>
</p>
<form name="form1" method="post" action="getsessionlist.jsp" onsubmit="return confirm('Are you sure want to clear the Session objects?');">
  <select name="loadtype">
    <option value="default" <%=chkopt(loadtype,"","selected")%>>Using Default</option>
      <option value="session.txt" <%=chkopt(loadtype,"session.txt","selected")%>>Using session.txt</option>    
      <option value="creditcard.txt" <%=chkopt(loadtype,"creditcard.txt","selected")%>>Using creditcard.txt</option>      
  </select>
  <input type="submit" name="Submit" value="Expire Session Now">
  <input type="button" name="Refresh" value="Refresh" onClick="javascript:window.location.href='getsessionlist.jsp?loadtype='+document.form1.loadtype.options[document.form1.loadtype.selectedIndex].value;">
</form>
<p>&nbsp; </p>
</body>
</html>


try amend accordingly, hope this helps.
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 17790579
generally, can try like this:

Enumeration enum = session.getAttributeNames();
Object curAttribute = null;
String attName = null;
while (enum.hasMoreElements())
{
    attName = (String)enum.nextElement();
    curAttribute = session.getAttribute(attName);
    out.println("Attribute name: " + attName);
    out.println("Type: " + curAttribute.getClass());
    out.println("<BR>");
}

http:Q_20874109.html
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 17790589
ahhh, post a wrong one.. what i mean is this..

java.util.Enumeration enum = session.getAttributeNames();
     Object curAttribute = null;
     String attName = null;
      Hastable ht=new Hashtable();
     while (enum.hasMoreElements())
     {
          attName = (String)enum.nextElement();
          curAttribute = session.getAttribute(attName);
          ht.put(curAttribute,attName);
    }

( sorry, angelIII )
0
 
LVL 49

Expert Comment

by:Ryan Chong
ID: 17790606
java.util.Enumeration enum = session.getAttributeNames();
     Object curAttribute = null;
     String attName = null;
     
    while (enum.hasMoreElements())
     {
          attName = (String)enum.nextElement();
          curAttribute = session.getAttribute(attName);
          out.println("Attribute name: " + attName);
          out.println("Attribute value: " + curAttribute);
          out.println("<BR>");
    }
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to count occurrences of each item in an array.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now