Question

I have the following code on my jsp file. In the two columns displayed, one column is a value and the other is a button. I want to modify the below code to pass the value in the row for which the button was clicked into a java file. The java file will basically utilize the passed value.

<table border="1" width="100%" cellpadding="0" cellspacing="0">
<tr>
<th>File ID</th>
<th>Generate Key</th>
</tr>
<%
 Connection con = DbConnector.getConnection();
 PreparedStatement pstm = null;
 String sql = "select u.uniquserid, t.filename, t.status, t.cloud, t.date_, t.report, t.FileID from transaction t join user u on u.userid = t.user order by t.date_ desc;";
 pstm = con.prepareStatement(sql);
 ResultSet rs = pstm.executeQuery();
 while (rs.next()) { %>

 <tr>
   <td><%=rs.getString(7)%></td>
        <% if (rs.getString(3).contains("s")) {%>
        <%request.getSession().setAttribute("PassFID", rs.getString(7));%>
   <td><input type="button" value='Generate Key' onclick=""></input></td>
   <%} else {%>
   <td></td>                                            
   <%}%>
 </tr>
 <%}%>
</table>
Was it helpful?

Solution

You can use javascript to call your function(I suggest you put the table in a form).

<tr>
 <td><%=rs.getString(7)%></td>
    <% if (rs.getString(3).contains("s")) {%>
    <%request.getSession().setAttribute("PassFID", rs.getString(7));%>
 <td><input type="button" value='Generate Key' onclick="onclick="somefunction(<%=rs.getString(7)%>);"></input></td>
  <%} 
 else {%>
 <td></td>                                            
 <%}%>

and then use a JS like below to pass the value to another jsp page. There on you can do whatever you like with this value.

 <script type="text/javascript">
    function somefunction(id){
        var f = document.form;
        f.method="post";
        f.action='somepage.jsp?id='+id;
        f.submit();
    }
</script>

OTHER TIPS

<tr>
   <td><%=rs.getString(7)%></td>
        <% if (rs.getString(3).contains("s")) {%>
        <%request.getSession().setAttribute("PassFID", rs.getString(7));%>
   <td><input type="button" value='Generate Key' onclick="yourFunction(<%=rs.getString(7)%>)"></input></td>
   <%} else {%>
   <td></td>                                            
   <%}%>
 </tr>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top