DImhoff1
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.
The same thing u can do for HasMap or any other objects also
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.j sp"%>
<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 ("producti d");
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></t d><td><b>P roduct 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(Int eger.parse Int(rs.get String("pr oductid")) );
myproduct.setDescription(r s.getStrin g("product descriptio n"));
myproduct.setFrequency(rs. getString( "frequency "));
myproduct.setProductcode(r s.getStrin g("product code"));
myproduct.setPrice(Integer .parseInt( rs.getStri ng("produc tprice"))) ;
list.add(myproduct);
%>
<tr>
<td><input type="text" size="2" name="qty" value="1"></td>
<td><input type="hidden" name="description" value="<%=rs.getString("pr oductdescr iption")%> "><%=rs.ge tString("p roductdesc ription")% ></td>
<td><input type="hidden" name="productcode" value="<%=rs.getString("pr oductcode" )%>"><%=rs .getString ("productc ode")%></t d>
<td><input type="hidden" name="productprice" value="<%=rs.getString("pr oductprice ")%>"><%=r s.getStrin g("product price")%>< /td><%
//total+= Integer.parseInt(rs.getStr ing("produ ctprice")) ;
index++;
%></tr>
<%}
request.setAttribute("thel ist",list) ;
%>
<tr>
<td> </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.j sp"%>
<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></t d><td><b>D escription </b></td>< td><b>Prod uct Code</b></td><td><b>Price< /b></td></ tr>
<%
LinkedList list=(LinkedList)request.g etAttribut e("thelist ");
%>
<%=list.size()%>
<tr>
<td> </td>
<td><b>Total: <b></td>
</tr>
</table>
</body>
</html>
__________________________ __________ __________ __________ __________ __________ __
What am I doing wrong?
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.
<TR>
<TD width="182" valign="top">
<TABLE width="100%">
<TR>
<%@ include file="includes/menu.jsp"%>
</TR>
<TR>
<%@ include file="includes/menu.login.
</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
int total = 0;
String myQuery = "SELECT * FROM product,fuelsource WHERE (";
for(int i=0;i<checkboxVals.length;
if(checkboxVals.length==i+
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></t
<form name="formEditQty" action="shopcart.jsp">
<%
LinkedList list = new LinkedList();
while(rs.next()){
Product myproduct = new Product();
myproduct.setProductid(Int
myproduct.setDescription(r
myproduct.setFrequency(rs.
myproduct.setProductcode(r
myproduct.setPrice(Integer
list.add(myproduct);
%>
<tr>
<td><input type="text" size="2" name="qty" value="1"></td>
<td><input type="hidden" name="description" value="<%=rs.getString("pr
<td><input type="hidden" name="productcode" value="<%=rs.getString("pr
<td><input type="hidden" name="productprice" value="<%=rs.getString("pr
//total+= Integer.parseInt(rs.getStr
index++;
%></tr>
<%}
request.setAttribute("thel
%>
<tr>
<td> </td>
<td><b>Total: <b></td>
<td><b><%=list.size()%></b
</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.
<TR>
<TD width="182" valign="top">
<TABLE width="100%">
<TR>
<%@ include file="includes/menu.jsp"%>
</TR>
<TR>
<%@ include file="includes/menu.login.
</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></t
<%
LinkedList list=(LinkedList)request.g
%>
<%=list.size()%>
<tr>
<td> </td>
<td><b>Total: <b></td>
</tr>
</table>
</body>
</html>
__________________________
What am I doing wrong?
ASKER
I set the list in a page here:_____________________ __________ _______
LinkedList list = new LinkedList();
while(rs.next()){
Product myproduct = new Product();
myproduct.setProductid(Int eger.parse Int(rs.get String("pr oductid")) );
myproduct.setDescription(r s.getStrin g("product descriptio n"));
myproduct.setFrequency(rs. getString( "frequency "));
myproduct.setProductcode(r s.getStrin g("product code"));
myproduct.setPrice(Integer .parseInt( rs.getStri ng("produc tprice"))) ;
list.add(myproduct);
%>
<tr>
<td><input type="text" size="2" name="qty" value="1"></td>
<td><input type="hidden" name="description" value="<%=rs.getString("pr oductdescr iption")%> "><%=rs.ge tString("p roductdesc ription")% ></td>
<td><input type="hidden" name="productcode" value="<%=rs.getString("pr oductcode" )%>"><%=rs .getString ("productc ode")%></t d>
<td><input type="hidden" name="productprice" value="<%=rs.getString("pr oductprice ")%>"><%=r s.getStrin g("product price")%>< /td><%
//total+= Integer.parseInt(rs.getStr ing("produ ctprice")) ;
%></tr>
<%}
request.setAttribute("prod uctlist",l ist);
and I try to get the list on another page here:_____________________ __________ __
<%
LinkedList list=(LinkedList)request.g etAttribut e("product list");%>
<%=list.size()%>
but list.size() gives me a null pointer exception................. ...
LinkedList list = new LinkedList();
while(rs.next()){
Product myproduct = new Product();
myproduct.setProductid(Int
myproduct.setDescription(r
myproduct.setFrequency(rs.
myproduct.setProductcode(r
myproduct.setPrice(Integer
list.add(myproduct);
%>
<tr>
<td><input type="text" size="2" name="qty" value="1"></td>
<td><input type="hidden" name="description" value="<%=rs.getString("pr
<td><input type="hidden" name="productcode" value="<%=rs.getString("pr
<td><input type="hidden" name="productprice" value="<%=rs.getString("pr
//total+= Integer.parseInt(rs.getStr
%></tr>
<%}
request.setAttribute("prod
and I try to get the list on another page here:_____________________
<%
LinkedList list=(LinkedList)request.g
<%=list.size()%>
but list.size() gives me a null pointer exception.................
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You are a lifesaver! Thank you!
FirstPage.jsp
--------------------------
LinkedList list=new LinkedList();
request.setAttribute("list
secondpage.jsp
-------------------
LinkedList list=(LinkedList)request.g
Regards,
gibu_george