Instantiating and adding Beans to an ArrayList
801424Nov 3 2009 — edited Nov 3 2009I 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 JSPs 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