2

I have a group of radio buttons each represent an action with an Id for each row. I am using value to compound the two. My servlet can handle this but I wonder is there any better way to separate action from id and still let servlet receive them?

  <td class="listData"><input type="radio" value="insert-${data.Id}" name="recordIds${data.Id}" id="${data.Id}"/></td>
  <td class="listData"><input type="radio" value="update-${data.Id}" name="recordIds${data.Id}" id="${data.Id}"/></td>
  <td class="listData"><input type="radio" value="delete-${data.Id}" name="recordIds${data.Id}" id="${data.Id}"/></td>
worpet
  • 3,788
  • 2
  • 31
  • 53
johnsam
  • 4,192
  • 8
  • 39
  • 58

1 Answers1

3

As Naga Sai A pointed out, you shouldn't have duplicate id values in your HTML document. So make sure that each input has a unique id.

If I understood you correctly, you don't need to use different values for each row data in your case. You need different names for them only:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Arrays" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<html>
<head>
  <title>Change Data</title>
</head>
<body>
  <%!
    public class Data {
      private int id;

      public Data(int id) {
        this.id = id;
      }

      public int getId() {
        return id;
      }

      public void setId(int id) {
        this.id = id;
      }
    }
    List<Data> dataList = Arrays.asList(new Data(10), new Data(11), new Data(12));
  %>

  <% request.setAttribute("dataList", dataList); %>
  <c:url var="controller" value="/controller"/>

  <p>Choose your actions for data</p>
  <form action="${controller}" method="POST">
    <table>
      <thead>
        <tr>
          <td>insert</td>
          <td>update</td>
          <td>delete</td>
        </tr>
      </thead>
      <tbody>
      <c:forEach var="data" items="${dataList}">
        <tr>
          <td>
            <label for="insert-${data.id}">Insert ${data.id}: </label>
            <input type="radio" name="recordIds${data.id}" value="insert" id="insert-${data.id}"/>
          </td>
          <td>
            <label for="update-${data.id}">Update ${data.id}: </label>
            <input type="radio" name="recordIds${data.id}" value="update" id="update-${data.id}"/>
          </td>
          <td>
            <label for="delete-${data.id}">Delete ${data.id}: </label>
            <input type="radio" name="recordIds${data.id}" value="delete" id="delete-${data.id}"/>
          </td>
        </tr>
      </c:forEach>
      </tbody>
    </table>
    <input type="submit" value="Submit actions">
  </form>
</body>
</html>

will give you a form

enter image description here

that will redirect its input to the servlet with doPost() method

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    Collections.list(request.getParameterNames()).forEach(paramName -> {
        if (paramName.startsWith("recordIds")) {
            String id = paramName.substring(9, paramName.length());
            String action = request.getParameter(paramName);
            try {
                response.getWriter().println("Selected action for data with id=" + id + " is " + action);
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    });
}

which prints the result

enter image description here

Ps: don't define Data class like this, the use of scriplets is highly discouraged, you should create your Data class separately and try to avoid Java code in JSP, i used it for demonstration purposes only

Community
  • 1
  • 1
mr.tarsa
  • 6,386
  • 3
  • 25
  • 42
  • Better like this indeed. `name` is used to find the item (the id only to stay simple ;) ), `value` to set the action and `id` is the combination of both (if an `id` is need). – AxelH Aug 03 '16 at 10:32
  • @AxelH Thanks for your suggestion. It indeed would be more simple if there are only these 3 input parameters (referring to 3 radio inputs). But what if there are other input parameters? Then on pulling these 3 parameters from `request.getParameterNames()` enumeration you will need to somehow distinguish them from those other parameters. That's why I did not remove that `recordIds` suffix from their names. But for this simple case you are right. – mr.tarsa Aug 03 '16 at 10:45
  • In general, for more complex table/list, I use an hidden input for the id value. And I analyze the div containing all the inputs for one item (div with a specific class to find a block). Other thing, using a dynamic name could be a problem to analyze the data of the POST request since you. (Just thought about it :P ) – AxelH Aug 03 '16 at 10:55