Html 表单、单选按钮和 Servlet

发布于 2024-07-27 00:57:07 字数 1097 浏览 1 评论 0原文

我编写了一个 servlet,它构建了一个显示数据库内容的 html 页面。 代码是:

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>");

我在每一行添加了一个单选按钮。 使用此代码,我可以在浏览器中显示包含数据库内容的表格。 当我单击提交时,我想将所选行的值“k”发送到另一个 servlet。 我很难受这个问题。 我认为我发送的值不正确。 在第二个 servlet 中,使用 getParameter() 来获取信息是否足够?

谢谢!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

盛夏尉蓝 2024-08-03 00:57:07

在第二个 servlet 中,您可以使用:

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

来读取值。 您需要知道要执行的操作的参数名称。 如果未知,您可以尝试枚举参数:

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

这只适用于具有单个值的参数。

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.

梦里寻她 2024-08-03 00:57:07

这条线正确吗?

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

Is this line correct?

out.println("<td>"+"<input type=\"radio\" name=\"titulo<%="+contador+"%>\"value=\""+k+"\">");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文