views:

44

answers:

1

Hi,

I've a HTML file that shows to the user the contents of a database (it is shown as a table). The user can choose one of the rows.When this is done the selection made by the user is sent to a servlet that will work with that information.

Imagine that this servlet is going to look for files related to the information chosen by the user. What I'd like to do is to provide the user with the option of also choosing the number of files that are going to be looked for by the servlet. That way the user should be able to choose one of the rows shown in the table and should also be able of typing the numers of files that are to be looked for.

So far I'm able to send to the servlet what the user chooses in the table, but I'd like to know if it is possible to attach to this information the number of files requested.

This is my code:

<center><form action="administ" method=post >                           
<center><table>
<table border=\"1\"><tr><th></th><th>Titleo</th><th>Author</th><th>Album</th></tr>
<c:forEach items="${items}" var="item">
<tr>                                
<td><input type="radio" name="Song" value="${item.file}@${item.title}#${item.author}$${item.album}">
<td>${item.title}</td>
<td>${item.author}</td>
<td>${item.album}</td>
</tr>
</c:forEach>
</table></center>
<tr><td colspan=2><input type=submit value = play name = option></td></tr>
<tr><td colspan=2><input type=submit value = Delete name = option></td></tr>

At this point I want to add a new option that requies not only a new button, but also requires the user to introduce a number.

Thanks

+1  A: 

That depends. If you want single selection of rows using radiobuttons, then you could just put a single input field at bottom of table, next the submit button or so. E.g.:

<input type="text" name="numberOfFiles">

which you can obtain in the servlet as follows:

String numberOfFiles = request.getParameter("numberOfFiles");

But if you want multiple selection of rows using checkboxes or if you want this field to appear in each row at any way, then you need to give the radio/checkbox field a value of the row index. If you're using JSTL <c:forEach> to iterate over the rows (which I'd expect that you indeed do), then you can make use of the varStatus attribute to declare a LoopTagStatus. Inside the loop you can obtain the row index by LoopTagStatus#getIndex(). E.g.:

<table>
    <c:forEach items="${items}" var="item" varStatus="loop">
        <tr>
            <td><input type="checkbox" name="selected" value="${loop.index}"></td>
            <td><input type="text" name="number"></td>
        </tr>
    </c:forEach>
</table>
<input type="submit">

(to have single selection, just replace type="checkbox" by type="radio")

In the servlet, you can obtain all input fields with the same name in the order as they appear in the table as follows:

String[] numbersOfFiles = request.getParameterValues("numberOfFiles");

With checkbox-selection you can obtain the all selected row indexes and thus also the associated input field as follows:

String[] selectedIndexes = request.getParameterValues("selected");
for (String selectedIndex : selectedIndexes) {
    int index = Integer.parseInt(selectedIndex);
    String numberOfFiles = numbersOfFiles[index];
    // ...
}

Or if it is a radiobutton-selected row which is single selection at any way:

String selectedIndex = request.getParameter("selected");
int index = Integer.parseInt(selectedIndex);
String numberOfFiles = numbersOfFiles[index];
BalusC
I'm adding part of my code to my question. When I show the table I present several options to the user, such as playing the audio file, deleting it from the database... Now I need to add another option which will let the user to select one of the files (one of the rows, I'm using radiobuttons) and also introduce a number. The servlet needs both the number and the row selected to work. What I think I need is to add one button at the end of the table that also allows me to introduce a number.
dedalo
Then add another input field outside the table next to the button? By the way, the `<center>` tag is deprecated since 1998. Use CSS `margin: 0 auto;`.
BalusC