Create object name using a String variable
Hi,
I磎 making a jsp page and I need to add several objects to a vector. The vector's name is arrayGrupo, and the object is Grupo. The problem is that when I create the instance of the object, I cant 磄ive him different names, so the add in the Vector will always be of the same object instance. Is there anyway to create an object name using a variable?
Something like this:
<% if (request.getParameter("grupoNome") != null ) {
nomeG=request.getParameter("grupoNome");
descG=request.getParameter("grupoDesc");
int nG=arrayGrupo.getTamanho(); //size of the array
String xpto=String.valueOf(nG);
Grupo "xpto"= new Grupo(); //here is the problem
xpto.setNomeGrupo(nomeG);
xpto.setDescricao(descG);
xpto.setIdgrupo(arrayGrupo.getTamanho()+1);
arrayGrupo.addGrupo(xpto);
}%>
Thanks
Rui Gon鏰lves
[893 byte] By [
pranxasa] at [2007-10-3 3:21:48]

When there's a good reason to associate values with names known only at run time, use a Map, usually a HashMap object. But I don't see any such reason here.
However I supect you are doing too much coding for a JSP anyway. The job of a JSP is to format HTML, not to read input. Use a servlet to process form input.
I want to show a list of objects that are stored in a vector. But I also want to add objects to that vector. If I just want to add one object, I can use this code:
<% if (request.getParameter("grupoNome") != null ) {
nomeG=request.getParameter("grupoNome");
descG=request.getParameter("grupoDesc");
Grupo newGrupo = new Grupo(nomeG,descG,arrayGrupo.getTamanho()+1);
arrayGrupo.addGrupo(newGrupo);
}
%>
What should I do if I want to add more than one object to the vector?
I don磘 know if I made myself clear but is quite hard to explain my problem.
Thanks
I've tryed that and it doesn磘 work. If I have three objects listed, and I add one, using the previous code, the object is added and it appears on the list. But if add another one, the object is added, but replacing the previous one. Do you see what I mean?
I think I see what your trying to do. I suspect your problem is that you are starting with a fresh, empty vector each time the JSP is run. Each time you submit a form that's a new transaction, and the JSP is run again from scratch. What you need to do is to preserve the vector of values between transactions, and the normal way to do that is to store it in "session".
In JSP you can use a "useBean" tag. Something like;
<jsp:useBean id="rowVector" class="java.util.Vector" scope="session"/>
Then you'll see a variable rowVector (don't declare it yourself) in your scriptlets which will survive from one transaction to the next.