Hi!! I'm using JSP+Servlets+JavaBeans to make a simple application of inventories.

I have created some beans in the servlet that contain the name of a factory , and then i want to get that data in a jsp page.

Since i'm having many names of factories i named the respective beans like this: factory1, factory2. I do this with this code:

String[] factories = CombofactoriesDB.select(); //this method gives me the names of the different factories in an array.

        for(int i = 0; i < factories.length; i++){
            request.setAttribute("factory" + i, new Factories(factories[i], null));//I "upload" the beans
        }

request.setAttribute("numberOfFactories", new Counter(factories.length));//This is the number of factories i've got, and i use a simple class that has only 1 attribute

Then in the JSP Page i have this code:

<jsp:useBean id="numberOfFactories" scope="request" class="Classes.Counter"/>
<%
    for(int i = 0; i < numberOfFactories.getCounter(); i++){
%>
<jsp:useBean id="factory<%=i>" scope="request" class="Classes.Factories"/>
<jsp:getProperty name="factory<%=i%>" property="name"/>
    <%}
%>

So in the last lines (useBean and getProperty) i want to input the number of the for cycle but i get an error of the generated servlet because it is interpreting the string as factory<%=i%> not factory0, factory1 and so on. Hope you can help me THANKS IN ADVANCE

hi there, i am no expert but if i understand your problem correctly then i would've add a string inide the jsp for loop and then call it in the usebean tag
this code below

<jsp:useBean id="numberOfFactories" scope="request" class="Classes.Counter"/>
<%
for(int i = 0; i < numberOfFactories.getCounter(); i++){
%>
<jsp:useBean id="factory<%=i>" scope="request" class="Classes.Factories"/>
<jsp:getProperty name="factory<%=i%>" property="name"/>
<%}
%>

should be something like this

<jsp:useBean id="numberOfFactories" scope="request" class="Classes.Counter"/>
<%
for(int i = 0; i < numberOfFactories.getCounter(); i++){
[B]string beanName= "factory"+i;[/B]
%>
<jsp:useBean id="<%=beanName>" scope="request" class="Classes.Factories"/>
<jsp:getProperty name="<%=beanName>" property="name"/>
<%}
%>

this way the concatenation will happend befor and then you will be able to have a dynamic change of factory names.
and if u define the String beanName befor the for loop it will be better.
i hope it works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.