• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

lose alredy created session bean

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all!
I have a problem with useBean and session. I use a OrderCartBean and this object contain an
ArrayList of OrdeBeans. I can add an OrderBean to OrderCartBean ArrayList. But next time I try to add an OrderBean to same OrderCartBean's ArrayList it does not work.
The problem is that I don't use the same OrderCartBean from session. The jsp-page seems to create a new
OrderCartBean instead of using the one in session and put the next OrderBean to the new OrderCartBean ArrayList
I looked around in many pages on the internet but i can't see what i'm doing wrong?
So I hope that someone here can help me.
Thanx in advance...

Here is my code:
[orderCart.jsp]
<jsp:useBean id="orderCart" scope="session" class="bean.OrderCart"/>
<html>
<body>
<form method="post" name="order" action="orderCart.jsp">
<table border=0 cellspacing=2 cellpadding=5 width=400>
<input name="action" type="hidden" value="addItemToOrder" />
<tr>
<td><label>Art.no</label>
<input type="text" name="artNo"></td>
<td><label>Name</label>
<input type="text" name="artName"></td>
<td><label>Quantity</label>
<input type="text" name="quantity" size="4"></td>
</tr>
<tr>
<td colspan=2 align=left><input type="submit" value="Add to order" name="AddValue"></td>
</tr>
<%
String action = request.getParameter("action");
if (action != null && action.equals("addItemToOrder")) {
//Set values to OrderBean
%>
<jsp:useBean id="order" class="com.easy.bean.Order" scope="request" />
<jsp:setProperty name="order" property="*" />
<%
//I got different orderCart every time. Not the same from session?
System.out.println(orderCart);
//Add order to OrderCart ArrayList
orderCart.addOrderToArray(order);
}
//Get orders from OrderCart array
Iterator iter = orderCart.getOrderArr().iterator();
while(iter.hasNext()){
com.easy.bean.Order tmpOrder = (com.easy.bean.Order)iter.next();
%>
<tr>
<td><B>Art.no.</B></td>
<td><B>Name</B></td>
<td><B>Antal</B></td>
</tr>
<tr>
<td><%=tmpOrder.getArtNo()%></td>
<td><%=tmpOrder.getArtName()%></td>
<td><%=tmpOrder.getQuantity()%></td>
</tr>
<%}%>
</table>
</form>
</body>
</html>

************************************************************************

[OrderCart.java]
package com.easy.bean;
import java.util.ArrayList;
public class OrderCart {

ArrayList<Order> orderArr = new ArrayList<Order>();

public OrderCart() {}
public ArrayList<Order> getOrderArr() {return this.orderArr;}
public void setOrderArr(ArrayList<Order> orderArr) {this.orderArr = orderArr;}
public void addOrderToArray(Order order) {this.orderArr.add(order);}
public void removeOrderFromArray(Order order) {this.orderArr.remove(order);}
}

************************************************************************

[Order.java]
package com.easy.bean;
public class Order {

int artNo;
int quantity;
String artName;

public Order() {}
public String getArtName() {return this.artName;}
public void setArtName(String artName) {this.artName = artName;}
public int getArtNo() {return this.artNo;}
public void setArtNo(int artNo) {this.artNo = artNo;}
public int getQuantity() {return this.quantity;}
public void setQuantity(int quantity) {this.quantity = quantity;}
}
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One question:
Why do you have :
<jsp:useBean id="orderCart" scope="session" class="bean.OrderCart"/>
instead of
<jsp:useBean id="orderCart" scope="session" class="com.easy.bean.OrderCart"/>
[ June 07, 2006: Message edited by: Satou kurinosuke ]
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And a little thing:
You can replace
if (action != null && action.equals("addItemToOrder"))
By
if ("addItemToOrder".equals(action))
 
Gorby Green
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Satou kurinosuke:
One question:
Why do you have :
<jsp:useBean id="orderCart" scope="session" class="bean.OrderCart"/>
instead of
<jsp:useBean id="orderCart" scope="session" class="com.easy.bean.OrderCart"/>

[ June 07, 2006: Message edited by: Satou kurinosuke ]



Ok. I see that i missed com.easy.bean in my post but i have it in my code.
But do you know why it create a new session instead of using the already created?
 
Gorby Green
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Satou kurinosuke:
And a little thing:
You can replace
if (action != null && action.equals("addItemToOrder"))
By
if ("addItemToOrder".equals(action))



Thanks! I will try that.
 
There will be plenty of time to discuss your objections when and if you return. The cargo is this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic