Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects in JSP

 
tejaswini vasist
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have a jsp which retieves an object and displays its contentsin the page.
The servlet written wil set the object values.I hav given print statements in the servlet.The output of which is correct and shows in Tomcat.But however the jsp doesnt show the same items even though i am retieving the same object and displayin it.What could be the problem?
jsp code:
<% DisplayItems items = new DisplayItems();
int numberOfItems=0;
String value = new String();
value = (String)request.getAttribute("NUMBER");
numberOfItems = Integer.parseInt(value);
items = (com.onjava.DisplayItems)request.getAttribute("ITEMS");
System.out.println("In jsp : " +numberOfItems);
try {
for (int j=0; j<numberOfItems; j++)
{
%>



<tr>

<td bgcolor=lightsteelblue><font face="verdana, arial, helvetica" size="2" width="20%"> <%=items.ItemID[j]%> </td>
<td bgcolor=lightsteelblue><font face="verdana, arial, helvetica" size="2" width="20%"> <%=items.ItemName[j]%> </td>
<td bgcolor=lightsteelblue><font face="verdana, arial, helvetica" size="2" width="20%"> <%=items.Quantity[j]%> </td>
</tr>
servlet code:
while(i<(vectorSize))
{
intElement = Integer.parseInt(listArray[i]);
try
{
conn= ct.getConnection();
}
catch(Exception e)
{
System.out.println("connection exception");
}
try
{
query = "select ItemID, ItemName, Price, QtyRemaining from Item where ItemID = '";
query = query + vect.elementAt(i) + "'";
stmt = conn.createStatement();
rs = stmt.executeQuery(query);
while(rs.next())
{
ItemID=rs.getInt("ItemID");
ItemName=rs.getString("ItemName");
Price=rs.getFloat("Price");
QtyRemaining=rs.getInt("QtyRemaining");
/*QtyRemaining=rs.getInt("QtyRemaining");
ThresholdQuantity=rs.getInt("ThresholdQuantity");*/
System.out.println(ItemID+"\t"+ItemName+"\t\t\t"+Price+"\t"+quantityArray[k]);
size = rs.getRow();
item.setItemID(ItemID);
item.setItemName(ItemName);
item.setPrice(Price);
item.setQuantityRemaining(QtyRemaining);
/*item.setThresholdQuantity(ThresholdQuantity);*/
dispitems.ItemID[k] = ItemID;
dispitems.ItemName[k] = ItemName;
dispitems.Quantity[k] = Integer.parseInt(quantityArray[k]);

System.out.println("***************************");
System.out.println("itemid:" + dispitems.ItemID[k]);
System.out.println("itemname:" + dispitems.ItemName[k]);
System.out.println("quant:" + dispitems.Quantity[k]);
} //end of while
the output of these print statements are correct but in jsp they show diff values.
the below code is a part of my dopost method:
DisplayItems dispitems1 = new DisplayItems();
SelectItems selitem = new SelectItems();

try
{
dispitems1 = selitem.dispSelectedItems(vect,quant);
}
catch(Exception e)
{
System.out.println("vector problem" + e.getMessage());
}


//Integer IntVal = new Integer(vectorSize1);
String str1=new String();
str1=Integer.toString(stcnt);
request.setAttribute("NUMBER", str1);
request.setAttribute("ITEMS", dispitems1);

// Forward the request to the target named
ServletContext context = getServletContext();
RequestDispatcher dispatcher =
context.getRequestDispatcher(target);
dispatcher.forward(request, response);
}
here i catch the object and convert it into response.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic