Tuesday, February 11, 2014

How to display all the session variables in an HTML page?

The session implicit object is an instance of javax.servlet.http.HttpSession. This variable is only valid for Http protocols. The session is one of the JSP built-in variables (like request) that is available in the service body of a JSP. 
There's no way you can do it directly from plain HTML since HTML is static and the session object really "lives" in the dynamic environment of the web container. The following example JSP shows you how to generate HTML that shows the objects in the session attribute collection.
<%@ page language="java" import="java.util.*" %>
<html>
<body>
Session attributes:
<%
  session.setAttribute("test.name", "Test Attribute List");
  session.setAttribute("test.float", new Float(5.0));
  session.setAttribute("test.int", new Integer(10));
  session.setAttribute("test.Object", new StringBuffer("StringBuffer"));
  session.setAttribute("test.boolean", new Boolean(true));
  session.setAttribute("test.double", new Double(343.1));
  for (Enumeration e = session.getAttributeNames(); e.hasMoreElements(); ) {     
    String attribName = (String) e.nextElement();
    Object attribValue = session.getAttribute(attribName);
%>
<BR><%= attribName %> - <%= attribValue %>

<%
}
%>
</body>
</html>

Original link: 
http://www.xyzws.com/JSPfaq/how-to-display-all-the-session-variables-in-an-html-page/33

No comments:

Post a Comment