Link to home
Start Free TrialLog in
Avatar of DImhoff1
DImhoff1Flag for United States of America

asked on

Need to pass Hashtable of objects to another jsp page via request or session

I need to pass a hashtable or linkedlist of objects into another jsp page via request or session variable.
Avatar of Gibu George
Gibu George
Flag of India image

Hello DImhoff1,

FirstPage.jsp
--------------------------------------
LinkedList list=new LinkedList();
request.setAttribute("listName",list)

secondpage.jsp
-------------------
LinkedList list=(LinkedList)request.getAttribute("listName");

Regards,

gibu_george
The same thing u can do for HasMap or any other objects also
Avatar of DImhoff1

ASKER

For some reason that is coming back null on the second page.

The code is:

1st page_____________________________________________________________________

<%@ page import="shopcart.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ include file="includes/header.jsp"%>
<%@ include file="includes/sub.header.2.column.jsp"%>

<TR>
      <TD width="182" valign="top">
            <TABLE width="100%">
                  <TR>
                        <%@ include file="includes/menu.jsp"%>
                  </TR>
                  <TR>
                        <%@ include file="includes/menu.login.jsp"%>
                  </TR>
            </TABLE>
      </TD>
      <TD width="30" rowspan="3"></TD>
      <TD valign="top">

            <TABLE border="1" width="100%">
                  <TR>
                        <TD colspan="7">
                              <FONT color="#3333CC">
                                    <H1>
                                          Please confirm your order
                                    </H1>
                              </FONT>
            <%String[] checkboxVals = request.getParameterValues("productid");
            int total = 0;
            
                  String myQuery = "SELECT * FROM product,fuelsource WHERE (";
                  for(int i=0;i<checkboxVals.length;i++){
                  if(checkboxVals.length==i+1){
                        myQuery += "productid=" + checkboxVals[i]+ "";}
                  else{
                        myQuery += "productid = " + checkboxVals[i] + " OR ";%>
                  <%}}%><br><%
                  myQuery += ") AND fuelsource.fuelsourceid = product.fuelsourceid";
                  OraDB mydb = new OraDB();
                  mydb.createConnection();
                  ResultSet rs;
                  rs = mydb.Query(myQuery);
                  %>
                  <table border="0" width="100%">
                  <tr><td><b>Quantity</b></td><td><b>Product Description</b></td><td><b>Product Code</b></td><td><b>Price</b></td></tr>
                  <form name="formEditQty" action="shopcart.jsp">
                  <%
                  LinkedList list = new LinkedList();
                  
                  while(rs.next()){
                  Product myproduct = new Product();
                  myproduct.setProductid(Integer.parseInt(rs.getString("productid")));
                  myproduct.setDescription(rs.getString("productdescription"));
                  myproduct.setFrequency(rs.getString("frequency"));
                  myproduct.setProductcode(rs.getString("productcode"));
                  myproduct.setPrice(Integer.parseInt(rs.getString("productprice")));
                  list.add(myproduct);
                  %>
                  <tr>
                  <td><input type="text" size="2" name="qty" value="1"></td>
                  <td><input type="hidden" name="description" value="<%=rs.getString("productdescription")%>"><%=rs.getString("productdescription")%></td>
                  <td><input type="hidden" name="productcode" value="<%=rs.getString("productcode")%>"><%=rs.getString("productcode")%></td>
                  <td><input type="hidden" name="productprice" value="<%=rs.getString("productprice")%>"><%=rs.getString("productprice")%></td><%
                  //total+= Integer.parseInt(rs.getString("productprice"));
                  index++;
                  %></tr>
                  <%}
                  request.setAttribute("thelist",list);
                  %>
                  <tr>
                  <td>&nbsp;</td>
                  <td><b>Total: <b></td>
                  <td><b><%=list.size()%></b></td>
                  </tr>
                  <tr><td colspan="6"><input type="submit"></td></tr>
                  <tr><td></td></tr>
                  </form>
            </table>
      </body>
</html>



2nd page___________________________________________________________

<%@ page import="shopcart.*"%>
<%@ page import="java.sql.*"%>
<%@ page import="java.util.*"%>
<%@ page import="java.lang.*"%>
<%@ include file="includes/header.jsp"%>
<%@ include file="includes/sub.header.2.column.jsp"%>

<TR>
      <TD width="182" valign="top">
            <TABLE width="100%">
                  <TR>
                        <%@ include file="includes/menu.jsp"%>
                  </TR>
                  <TR>
                        <%@ include file="includes/menu.login.jsp"%>
                  </TR>
            </TABLE>
      </TD>
      <TD width="30" rowspan="3"></TD>
      <TD valign="top">

            <TABLE border="1" width="100%">
                  <TR>
                        <TD colspan="7">
                              <FONT color="#3333CC">
                                    <H1>
                                          Edit Quantities
                                    </H1>
                              </FONT>
            
                  <table border="0" width="100%">
                  <tr><td><b>Quantity</b></td><td><b>Description</b></td><td><b>Product Code</b></td><td><b>Price</b></td></tr>
                  <%
                  LinkedList list=(LinkedList)request.getAttribute("thelist");
                   %>
                  <%=list.size()%>
                  
                  <tr>
                  <td>&nbsp;</td>
                  <td><b>Total: <b></td>
                  </tr>
            </table>
      </body>
</html>


______________________________________________________________________________

What am I doing wrong?
I set the list in a page here:______________________________________

LinkedList list = new LinkedList();
                  
                  while(rs.next()){
                  Product myproduct = new Product();
                  myproduct.setProductid(Integer.parseInt(rs.getString("productid")));
                  myproduct.setDescription(rs.getString("productdescription"));
                  myproduct.setFrequency(rs.getString("frequency"));
                  myproduct.setProductcode(rs.getString("productcode"));
                  myproduct.setPrice(Integer.parseInt(rs.getString("productprice")));
                  list.add(myproduct);
                  %>
                  <tr>
                  <td><input type="text" size="2" name="qty" value="1"></td>
                  <td><input type="hidden" name="description" value="<%=rs.getString("productdescription")%>"><%=rs.getString("productdescription")%></td>
                  <td><input type="hidden" name="productcode" value="<%=rs.getString("productcode")%>"><%=rs.getString("productcode")%></td>
                  <td><input type="hidden" name="productprice" value="<%=rs.getString("productprice")%>"><%=rs.getString("productprice")%></td><%
                  //total+= Integer.parseInt(rs.getString("productprice"));
            
                  %></tr>
                  <%}
                  request.setAttribute("productlist",list);


and I try to get the list on another page here:_________________________________

<%
                  LinkedList list=(LinkedList)request.getAttribute("productlist");%>
                  <%=list.size()%>





but list.size() gives me a null pointer exception....................
ASKER CERTIFIED SOLUTION
Avatar of Gibu George
Gibu George
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are a lifesaver! Thank you!