![]() |
![]() |
![]() |
Hash table: Servlet side:Hashtable ht = new Hashtable(); ResultSet rsc = db.selectdb(cases_sql_1); // run cases_sql_1 query to select from the database try{ while (rsc.next()){ ht.put(rsc.getString(1), rsc.getString(3)); // placing 1st field as a key and 3rd field as a value from the // selected rows into the hashtable. Instead of field name you // place it by field name // ht.put(rsc.getString("CUST_NO"),rsc.getString("CASES")) } catch (Exception e {System.out.println(e.Message()); } req.setAttribute("ht",ht) // passing to the jsp as a parameter // You can also set session as session.setAttribute("ht",ht) //To see the data for (Enumeration e = ht.keys(); e.hasMoreElements();) {object_key = (String) e.nextElements(); // Get the key's System.out.println("Key = " + object_key + " Value = " + ht.get(object_key)); / Get the Value of the key. } jsp side <%@ page import = "java.util.*" %> <%@ page import = "java.io.*, java.util.Hashtable" %> <% Hashtable ht = new Hashtable(); ht = (Hashtable) request.getAttribute("ht"); //getting ht from passed parameter by servlet //Getting from session - ht=(Hashtable) session.getAttribute("ht"); //You do not have to use following loop this is just for just understaning enumeration loop for (Enumeration e = ht.keys(); e.hasMoreElements();) {object_key = (String) e.nextElements(); // Get the key's System.out.println("Key = " + object_key + " Value = " + ht.get(object_key)); / Get the Value of the key. %> <TD>Cases value for cust is <%ht.get(cust_no)%><TD> // In this case I am setting cust_no from selected vector //<%String cust_no; //Vector vec = new Vector() //vec = (Vector) request.getAttribute("OP"); //for (int i = 0; i<vec.size(); i++} // {CustProfileHistoryBean cph = (CustomerProfileHistoryBean) vec.elementAt(i); // cust_no = cph.getcustomer_no();} <TD>Cases value for cust is <%ht.get(cust_no)%><TD> |