0

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

Edited by mike_2000_17: Fixed formatting

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by kakashi_sensi
0

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.

Edited by kakashi_sensi: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.