views:

984

answers:

2

Hi, I've writen a servlet that builds an html page showing the content of a database. The code is:

Statement st = (Statement) conexion.createStatement();                     
ResultSet rs = st.executeQuery("select * from audiolist" );
while (rs.next())
   {
    contador++;
    out.println("<tr>");
    String k = rs.getString("Tittle");
    String l = rs.getString("Autor");
    String m = rs.getString("Album");
    out.println("<td>"+"<input type=\"radio\" name=\"titulo<%="+contador+"%>\"value=\""+k+"\">");
    out.println("<td>" + k + "</td>");         
    out.println("<td>" + l + "</td>");
    out.println("<td>" + m + "</td>");    
    out.println("</tr>");
    }
    out.println("</table></center>");
    out.println("<tr><td colspan=2><input type=submit></td></tr>");
    out.println("</form>");

I've added a radio button to each row. With this code I get to show in the browser a table with the content of the database. When I click on submit I want send to another servlet the vale 'k' for the row selected. I'm having a hard time with this. I think I'm sending the value incorrectly. In the second servlet, is it enough to use getParameter() in order to get the info?

Thanks!

A: 

In the second servlet you can use:

String value = request.getParameter("tituloX");

to read the value. You need to know the name of the parameter to do. If this is not known, you can try to enumerate the parameters:

for ( Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
  String param = (String) e.nextElement();
  String value = request.getParameter(param );
}

This only works for parameters with a single value.

kgiannakakis
I still don't understand it. What's what is transmmited in the request object, what's the format? When I select a radio button and click on the submit button, is it not just that radio button what's transmitted to the servlet? If I select button in row number 1 and in the servlet use: String value = request.getParameter("titulo1");When I print the value of 'value' it says it is null
Use method="GET" at your form. When submitting you should see a URL like myservlet?titulo1=k at the address bar. The parameters are passed to the servlet URL encoded at the request path. With POST method they are inserted at the body of the request, but it is actually the same thing.
kgiannakakis
A: 

Is this line correct?

out.println("<td>"+"<input type=\"radio\" name=\"titulo<%="+contador+"%>\"value=\""+k+"\">");
dedalo