[JSP]

S

Stefan Richter

I am connecting to a database, and based on the values of the database
I am dynamically creating checked or unchecked checkboxes.

allTestsList = database.getTests(10);
for (Enumeration el = allTestsList.elements(); el.hasMoreElements();
) {
testBean = (TestBean)el.nextElement();
test = testBean.getTest();
out.println("<INPUT TYPE=CHECKBOX NAME=\""+
testBean.getTestShortName() +"\" ");

if (testBean.hasSignedUp()) {
out.println("CHECKED>");
}
else {
out.println(">");
}
out.println(test);
}

Then the user can check or uncheck them and I want to update the
database again.
For my sql string I first of all need a string that contains all
values seperated by commas and ''.

for (Enumeration el = request.getParameterNames();
el.hasMoreElements(); ) {
String param = (String)el.nextElement();
if (! "submit".equals(param)){
checkedTests += "'"+ param +"', ";
}

}

The problem now is that I will have one comma to much at the end when
I am doing it that way.
What's the best / easiest way to solve that?

Anyway, so I "clean up" like this:

if(checkedTests !=null && !"".equals(checkedTests ) ) {
checkedTests = checkedTests.substring(0, checkedTests.length() -2
);
}

But I think it looks pretty crappy coded.

Any way to do that in a better way?


Thanks,

Stefan
 
A

anonymous

Stefan said:
I am connecting to a database, and based on the values of the database
I am dynamically creating checked or unchecked checkboxes.

allTestsList = database.getTests(10);
for (Enumeration el = allTestsList.elements(); el.hasMoreElements();
) {
testBean = (TestBean)el.nextElement();
test = testBean.getTest();
out.println("<INPUT TYPE=CHECKBOX NAME=\""+
testBean.getTestShortName() +"\" ");

if (testBean.hasSignedUp()) {
out.println("CHECKED>");
}
else {
out.println(">");
}
out.println(test);
}

Then the user can check or uncheck them and I want to update the
database again.
For my sql string I first of all need a string that contains all
values seperated by commas and ''.

for (Enumeration el = request.getParameterNames();
el.hasMoreElements(); ) {
String param = (String)el.nextElement();
if (! "submit".equals(param)){
checkedTests += "'"+ param +"', ";
}

}

The problem now is that I will have one comma to much at the end when
I am doing it that way.
What's the best / easiest way to solve that?

Anyway, so I "clean up" like this:

if(checkedTests !=null && !"".equals(checkedTests ) ) {
checkedTests = checkedTests.substring(0, checkedTests.length() -2
);
}

But I think it looks pretty crappy coded.

Any way to do that in a better way?


Thanks,

Stefan

It occurred to me that you already have a testBean variable. I usually
supply the Enumeration from the request.getParameterNames to the xtor of
an object that resembles the data I expect form the user. The xtor sets
the fields of the object and it is easy to create a method on the object
that will either return the dynamic SQL string necessary or to even
update itself in the database.

This approach also makes it simple to test the validity of all the user
supplied data. At the same time it keeps your JSP relatively clean and
crisp.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,888
Messages
2,569,964
Members
46,294
Latest member
HollieYork

Latest Threads

Top