aspose file tools*
The moose likes Struts and the fly likes put arraylist in session? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "put arraylist in session?" Watch "put arraylist in session?" New topic
Author

put arraylist in session?

Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
Hi everyone:

There is a problem puzzled me.I query database and get resultset in servlet.I put the resultset in arraylist and put them to session.But it is waste too many memory.I will using the variable in jsp page so I put the variable in session.It is right?How to design the resultset? Thks :roll:


When I face C# and Java I choose Java.
Bernardus Irmanto
Greenhorn

Joined: Oct 03, 2003
Posts: 23
yashnoo,

You should wrap the data retrieved from the database in to a bean. In the your action form, you can define a collection which contains the bean.
You can then use the <logic:iterate > tag in your jsp to output the data

rgds
beN
Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
Thank you.
I method is query all the data to a javabean and put the javabean to ArrayList.But I count a problem.
I have know the problem but I don't know how to solve it.The problem is in my Action class.I query data from database successfully in Action.The Tomcat console can display the data when I run the jsp file but the jsp can't display the data.So I test it and I found that the method "getListarticle" return null.
The most important thing is that if I change the "listarticle" to static method the jsp can display data! But when I fresh the page the data become twice and so on.
But the problem is in the method "findData()" the listarticle isn't null.Does the JVM initial the listarticle to null after process the "execute()" method?How to solve it?
my Action code is:
/////////////////////////////////////////////////////////////////////////////
public class Savelovearticle extends Action{
DataSource ds=null;
ResultSet rs=null;
Statement stm=null;
Connection conn=null;
String sql="select * from loveforum";
public List listarticle=new ArrayList(); //muse be static ?

Lovearticlebean lovebean=null;
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response
)throws Exception{
ActionMessages errors=new ActionMessages();
try{
this.findData(request);
}catch(Exception e){
System.out.println(e.toString());
errors.add("lovearticle.create resultset error",new ActionMessage("error.sql.createlovearticle"));
}
if(!errors.isEmpty()){
this.saveErrors(request,errors);
return mapping.getInputForward();
}
return mapping.findForward("success");
}
public List getListarticle(){
//System.out.println(this.listarticle.toString());
return this.listarticle;
}
public void setListarticle(List listarticle){
this.listarticle=listarticle;
}
//find data from database
public void findData(HttpServletRequest request){
try{
ds=(DataSource)this.getDataSource(request,"mydatasource");
conn=ds.getConnection();
stm=conn.createStatement();
rs=stm.executeQuery(sql);
while(rs.next()){
lovebean=new Lovearticlebean();
System.out.println("Enter while rs.next():");
lovebean.setId(rs.getString("id"));
System.out.println("loveid is:"+lovebean.getId()); //debug: this can output ID from fatabase
lovebean.setTitle(rs.getString("title"));
lovebean.setName(rs.getString("name"));
lovebean.setContent(rs.getString("content"));
lovebean.setTime(rs.getString("time"));
lovebean.setFatherid(rs.getString("fatherid"));
listarticle.add(lovebean);
}
System.out.println("Enter iterate:"+listarticle.toString()); //debug
if(listarticle!=null){
System.out.println("Enter list!=null"); }
if(rs!=null)
rs.close();
if(stm!=null)
stm.close();
if(conn!=null)
conn.close();
}catch(Exception e){
System.out.println(e.toString());
}
}

}
My code have some mistake? :roll:
[ October 06, 2003: Message edited by: Yashnoo lyo ]
Bernardus Irmanto
Greenhorn

Joined: Oct 03, 2003
Posts: 23
Hi there
It's a little bit hard to read yer code..so I'll try to formulate my own solution to yer problem(based on yer code). Below is how I will do it :
1. create a bean (well you can name it "loveBean" if you want)
the bean has the following private members :
-id
-title
-name
-content
-time
-fatherid
2. I will define a method, say getData which return ArrayList
in this method I will query the database, and iterate through the resultset, wraps each resultset in to bean object, and put the bean in to the arraylist. (suggestion: separating the code which query the data, and the code which wraps yer data will ease you to maintain yer code)
public ArrayList getData(...)throws SQLException {
...
}

3. I will define a class which extends ActionForm.(say, loveForm).
in the loveForm, I will define a ArrayList Variable.

public class loveForm extends ActionForm {
...
private ArrayList loves;
...//other vars

public void setLoves(ArrayList arr){
loves=arr;
}
public ArrayList getLoves(){
return loves
}

public void setLove(int index, loveBean love){
loves.add(index,love)
}
public loveBean getLove(int index){
return (loveBean) loves.get(index)
}
...
}

4. In the "execute" method of the Action class, I will call the getData method, set the Arraylist returned by the method in to loveForm, and put the form in the request object
loveForm myForm.setLoves(getData(...))
obRequest.setAttribute(Constant.LOVE_FORM,myForm)
5. In my jsp I will use the logic iterate to output the data
<logic:iterate name="loveForm" property="loves" id="row" indexId="index" scope="request" >
and then I can output the data using html:text tag/bean:write tag
ex(to output title):
<html:text property='<%= "loves["+index+"].title" %>' />

welll..that's how i will do it..

rgds
beN
Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
Thank you for your reply
I want to take a test using struts now.I will write query database code using DAO or Hibernate later.
1.You means that put all the data into the "request"?I think it is waste memoy as put them into "session".Have another method?

2.As I know, the field that in the ActionForm must be "string" or "Integer".right? :roll:
I don't know whether I say is right.I will wait your suggest...
Bernardus Irmanto
Greenhorn

Joined: Oct 03, 2003
Posts: 23
Hi,

as far as I know, that's how Struts comunicates the data from the back end to the front end. There are several ways to keep the state between the subsequent requests. Put data in to the session is one way, or you can user the statefull session bean (EJB), but still u wont get away from the memory/bandwidth problem. If you set the scope of your form to "request", then your data will last as long as the life time of the request object.
(will not consume certain amount of memory for significant amount of time)
So, I think the prformance will still be acceptable.(You can test it if you want, then u can tell me how it run)
well, the variables in your from are not neccesarily String and integer. You can have beans , or collections. Having both will simplify your code, and help you to manage your objects.(well it's object oriented approach, isnt it?). I myself prefer to create a bean whose all variables are string(since they re all will corresponds to the field in the jsp. Using String var will enable me to format the value for presentation).
Hope it will help you
rgds
beN
Yashnoo lyo
Ranch Hand

Joined: Feb 17, 2003
Posts: 152
Hi
I have try it already.If I use "request" I find it will take 6M memory,it isn't much.It can work property.
I means that the struts ActionForm's field must be String. And the common javabean may have other type like List,Vector.right?
Thanks
Krunal Shah
Greenhorn

Joined: Mar 03, 2006
Posts: 2
Originally posted by Bernardus Irmanto:
Hi there

It's a little bit hard to read yer code..so I'll try to formulate my own solution to yer problem(based on yer code). Below is how I will do it :

1. create a bean (well you can name it "loveBean" if you want)
the bean has the following private members :
-id
-title
-name
-content
-time
-fatherid

2. I will define a method, say getData which return ArrayList
in this method I will query the database, and iterate through the resultset, wraps each resultset in to bean object, and put the bean in to the arraylist. (suggestion: separating the code which query the data, and the code which wraps yer data will ease you to maintain yer code)

public ArrayList getData(...)throws SQLException {
...
}


3. I will define a class which extends ActionForm.(say, loveForm).

in the loveForm, I will define a ArrayList Variable.


public class loveForm extends ActionForm {

...

private ArrayList loves;
...//other vars


public void setLoves(ArrayList arr){

loves=arr;
}

public ArrayList getLoves(){

return loves
}


public void setLove(int index, loveBean love){
loves.add(index,love)
}

public loveBean getLove(int index){

return (loveBean) loves.get(index)
}

...

}


4. In the "execute" method of the Action class, I will call the getData method, set the Arraylist returned by the method in to loveForm, and put the form in the request object

loveForm myForm.setLoves(getData(...))
obRequest.setAttribute(Constant.LOVE_FORM,myForm)

5. In my jsp I will use the logic iterate to output the data
<logic:iterate name="loveForm" property="loves" id="row" indexId="index" scope="request" >

and then I can output the data using html:text tag/bean:write tag
ex(to output title):
<html:text property='<%= "loves["+index+"].title" %>' />


welll..that's how i will do it..


rgds

beN
Krunal Shah
Greenhorn

Joined: Mar 03, 2006
Posts: 2
I have tride that code exactly but it is still giving me error that..
"Cannot find bean Loves "

so is there any solution for that..
 
jQuery in Action, 2nd edition
 
subject: put arraylist in session?
 
Similar Threads
Need help with HashMap logic
Collection on jsp
Assigning a DB object to a JSP variable
login code repeats error message
Writing Resultset to File - best way?