单选按钮及其在html表(对应行)中与servlet相关联的值

时间:2015-07-14 06:31:42

标签: html jsp servlets

<HTML>
    <HEAD>
        <TITLE>Report</TITLE>
    </HEAD>

    <BODY>
    <form action=allrep method="get">
          <H1>Report</H1>
        <% 
        String url = "jdbc:mysql://localhost:3306/fees";
        String user = "root";
        String passswd = "password";
        Connection connection = DriverManager.getConnection(url,user,passswd);
        Statement statement = connection.createStatement();
        ResultSet resultset = 
        statement.executeQuery("select * from fee1") ; 
        %>
        <TABLE BORDER="1">
            <TR>
            <th>Select</th>
            <TH>Id</TH>
            <TH>Name</TH>
            <TH>Fee</TH>
            <TH>Course</TH>
            <TH>Occupation</TH>
            <TH>Balance</TH>
            <TH>Date</TH>
            </TR>
           <%     while (resultset.next()) { %>
           <TR>
           <td><input type="radio" name="setval"></td>
           <TD> <%= resultset.getString(5) %> </TD>
           <TD contenteditable="true"> <%= resultset.getString(1) %> </TD>
           <input type="hidden" value="<%= resultset.getString(1) %>" name="fname">
           <TD contenteditable="true"> <%= resultset.getString(2) %> </TD>
           <TD contenteditable="true"> <%= resultset.getString(3) %> </TD>
           <TD contenteditable="true"> <%= resultset.getString(4) %> </TD>
           <TD contenteditable="true"> <%= resultset.getString(6) %> </TD>
           <TD contenteditable="true"> <%= resultset.getString(7) %> </TD>
           </TR>
       <% 
           } 
       %>
       </TABLE>  <BR><BR>
        <input type="submit" value="Update Record">  
        </form>
    </BODY>
</HTML>

这是servlet代码:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class allrep
 */
public class allrep extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public allrep() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String radio=request.getParameter("setval");

        if(radio!=null)
        {
            String fname=request.getParameter("fname");
            System.out.println("you've ticked");
            System.out.println(fname);
        }
        // TODO Auto-generated method stub
    }

}

在这里,无论单选按钮如何,fname只打印第一行的值。

当我按下第二个单选按钮时,我想获取该特定行的值,但我只是在使用System.out.printf时获得第一行的值。

1 个答案:

答案 0 :(得分:1)

radiobuttons背后的想法是通过设置<input type="radio name="setval>属性为每个value元素提供不同的值。然后,浏览器将所选单选按钮的值放入已定义名称的参数中。

因此请像这样使用radiobutton并删除隐藏的参数:

<input type="radio" name="setval" value="<%= resultset.getString(1) %>" />

然后检索它:

String fname=request.getParameter("setval");