File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes lose alredy created session bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "lose alredy created session bean" Watch "lose alredy created session bean" New topic
Author

lose alredy created session bean

Gorby Green
Greenhorn

Joined: Nov 23, 2002
Posts: 12
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;}
}
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

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 ]

[My Blog]
All roads lead to JavaRanch
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

And a little thing:
You can replace
if (action != null && action.equals("addItemToOrder"))
By
if ("addItemToOrder".equals(action))
Gorby Green
Greenhorn

Joined: Nov 23, 2002
Posts: 12
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

Joined: Nov 23, 2002
Posts: 12
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: lose alredy created session bean