1

i'm having some troubles with my code and i don't know how to continue.

What i have is a web page that shows a list of tweet previously searched by the user.

Now what i want is to that the user could give some information about the search, something like if it was helpfull and in what area(sport, shopping, computing..) should it be.

The code i have is something this:

<table class="display dataTable no-footer" id="DataTables_Table_0">
                        <%
                            Class.forName("com.mysql.jdbc.Driver");
                            Connection connect = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "test", "test");
                            Statement statement = connect.createStatement();
                            String query = "select * from cataluna_sin;";
                            ResultSet rs = statement.executeQuery(query);
                            while (rs.next()) {
                        %>
                        <tr>
                        <tr>
                            <td><b>tweet</b></td>
                            <td><b>retweet</b></td>
                            <td><b>usuario</b></td>
                            <td><b>Geolocalización</b></td>
                            <td><b>creador</b></td>
                            <td><b>¿Relevante?</b></td>
                            <td><b>Tipo de Delito</b></td>
                        </tr>
                        <%
                            while (rs.next()) {
                        %>
                        <tr>
                            <td><%=new String(rs.getString("tweet").getBytes("ISO-8859-1"), "UTF-8")%></td>
                            <td><%=rs.getInt("retweets")%></td>
                            <td><%=rs.getString("usuario")%></td>
                            <td><%=rs.getString("geolocalizacion")%></td>
                            <td><%=rs.getString("creador")%></td>
                            <td>

                                <form action=Update onsubmit=refreshpage();return false>
                                    <input type="radio" value="No" name="<%=rs.getLong("id")%>"
                                        checked>No<br> 
                                    <input type="radio" value="Si"
                                        name="<%=rs.getLong("id")%>">Si<br>
                            </td>
                            <td></td>
                            <td>
                            <input type="submit" value="clasificar"></td>
                            </form>
                        </tr>
                        <%
                            }
                            }
                        %>
                    </table>

what i want i to update the database when they press the submit button, but i don't know how.

Osama AbuSitta
  • 3,918
  • 4
  • 35
  • 51
Fernando
  • 15
  • 3
  • Every radio button you want saved has to be given a unique name. Then on the server, to process your form you should call request.getParameter("uniqueId") etc. – dsp_user Jan 13 '18 at 20:33

1 Answers1

0

In your code you have this

<input type="radio" value="No" name="<%=rs.getLong("id")%>" checked>No<br> 
<input type="radio" value="Si" name="<%=rs.getLong("id")%>">Si<br>

Change it to

<input type="radio" value="No" name="radio_<%=rs.getLong("id")%>" checked>No<br>
<input type="radio" value="Si" name="chk_<%=rs.getLong("id")%>">Si<br>

In this way you will be able to identify them differently by using radio_id and chk_id otherwise in your case both will have same name and you will not be able to identify their values.

Furthermore

You have to post the form to a url and the url should map to a servlet.

Your database code should be in the servlet, Not in the jsp.

In this case it might also be possible by submitting the form to the same url but its not a good idea.

You have refreshPage javascript method in the form onsubmit. You have post the data via ajax or via page postback to the servlet.

So

  1. Create a servlet. Put all your database code in it.
  2. Remove the db code from jsp
  3. Load the data from the servlet
  4. And to save just point the submit form to the servlet (same servlet that loaded data or another one)
  5. After that in the servlet on successful db save redirect back to this jsp
muasif80
  • 5,586
  • 4
  • 32
  • 45
  • The DB code could still be in the jsp page, but it's discouraged. It makes for a much better desing to leave just the GUI in the jsp and move everything else in the servlet. – dsp_user Jan 13 '18 at 20:35