Discussion Locked

Java servlets

0 helpful
4 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.

I am beginning to use servlets through an Apache web server. I understand how the servlet interacts with the posts from the web page users overall, but I am not sure of how to handle multiple selection boxes. How do you receive all of the responses from the post for the multiple selections? How do they come through? (As comma seperated with the variable name listed once, or as seperate entries with the same variable name) Any help is appreciated.

Jeremy

4 replies

by rakesh goenka

The response from multiple selections (as in a multi-select list), will be name=&name=&name=
as in
accounts=1&accounts=2&accounts=3

The HTTPRequest object has a getParameterValues() method which allows to get the multiple values by supplying the name. Here is a code example where I get multiple account selections for a report and convert them into a long[]:

....
String[] accounts = request.getParameterValues( "accounts" );
long[] accountIDs = null;
if ( accounts != null ) {
int count = accounts.length;
accountIDs = new long[ count ];
for ( int i = 0; i < count; i++ ) {
accountIDs[ i ] = Long.parseLong( accounts[ i ] );
}

// work with account ids further
....

I have found an issue where I was using multi-select by using same name for checkboxes, but a few browsers only returned the first selection of the check box. A multi-selection list is compatible with all browsers.

Hope that helps,

This discussion is closed to new replies.