Can a JSP page access data in a Hashtable instead of a bean? If so, what is the syntax? For example, I want to put simple values into the Hashtable as well as more complex values such as other Hashtables or Vectors and retrieve them by key.

Govind Seshadri

You can use just about any Java class within a JSP page. If the class does not subscribe to JavaBean design patterns (i.e. has no getter/setter methods for properties) it cannot be used within the <jsp:getProperty> and <jsp:setProperty> tags, and has to be accessed only within scriptlets.

The following source code demonstrates the usage of Vector and Hashtable objects within JSP scriptlets:

Page1.jsp creates a Hashtable and initializes it with some data before placing it into the session:

<%@ page import="java.util.*"%>
<jsp:useBean id="myHash" class="java.util.Hashtable"
Hashtable numbers = new Hashtable();
numbers.put("one", new Integer(1));
numbers.put("two", new Integer(2));
numbers.put("three", new Integer(3));

Vector cities = new Vector();
cities.addElement("San Francisco");

Placed hashtable within the session...

Page2.jsp extracts the previously instantiated Hashtable instance from the session and displays its contents:

<%@ page import="java.util.*"%>

<jsp:useBean id="myHash"
  scope="session" />

Printing out the contents of Hashtable instance 
extracted from the session:
Hashtable newNumbers = 
if (newNumbers != null) {
  Enumeration e = newNumbers.keys();
  Collection c = newNumbers.values();
  Iterator i = c.iterator();
  while (i.hasNext()) {
    Integer intValue = (Integer);

Vector newCities = (Vector)myHash.get("cities");
if (newCities != null) {
  for (int i=0; i newCities.size();i++)

0 Comments  (click to add your comment)
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



About | Sitemap | Contact