Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Instantiating and adding Beans to an ArrayList

801424Nov 3 2009 — edited Nov 3 2009
I want to have several beans instantiated from the same class each containing different data all stored in an ArrayList?
I know that it can be done by just directly placing the instantiating line directly in the loop so that on each iteration a new class is instantiated then placed inside the ArrayList. But in JSP’s beans are normally declared at the top. Is it acceptable to use a line like the following inside a jsp or is there another way to do this?
DataBean dataBean = new DataBean();

I have added the code below to further explain what I mean. In the code below both elements in the ArrayList will contain objects with identical data.
Thanks,

<jsp:useBean id = "dataBean" class ="BeanPlace.DataBean"/>


ArrayList arList = new ArrayList();

dataBean.setID("1111");
dataBean.setName("mark");
dataBean.setPlace("US");
arList.add(0, dataBean);

dataBean.setID("222");
dataBean.setName("russel");
dataBean.setPlace("Uk");
arList.add(1, dataBean);

session.setAttribute("aaas", arList
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Dec 1 2009
Added on Nov 3 2009
2 comments
355 views