Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 237
  • Last Modified:

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.
0
DImhoff1
Asked:
DImhoff1
  • 3
  • 3
1 Solution
 
Gibu GeorgeCTOCommented:
Hello DImhoff1,

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

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

Regards,

gibu_george
0
 
Gibu GeorgeCTOCommented:
The same thing u can do for HasMap or any other objects also
0
 
DImhoff1Author Commented:
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?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
DImhoff1Author Commented:
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....................
0
 
Gibu GeorgeCTOCommented:
that is because the object will be in request only for the first requst, i.e the submit ot the second page, if you want it across the session you need to put it session using session.setAttribute("productlist",list), ad getting it using session.getAttribute("productlist")
0
 
DImhoff1Author Commented:
You are a lifesaver! Thank you!
0

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now