Skip to content Skip to sidebar Skip to footer

The Way To Get A Value From A Hidden Type Correctly

in a html table i construct in each row an edit button like the following: retour.append('

Solution 1:

You can send the id parameter with the GET HTTP method in each row:

<ahref="[URL]?id=[id]"><imgsrc="edit.gif"/></a>

where:

  • URL is the URL to which you submit this form to and
  • id is what you build with "id_" + nomTab + "_" + compteur

Solution 2:

You can get the id from the HttpServletRequest request variable in doGet()/doPost() methods using the getParameter() method. Example: request.getParameter("edit"). "edit" is the name of the input field.

Your html code is not valid. You should quote your attributes. Also you might consider doing the html output in JSP instead of appending strings in a servlet.

Like Bruno said. It might be easyer to create href links width an id request parameter instead of forms with hidden input fields.

Solution 3:

You're unnecessarily overcomplicating things. First, HTML should not be emitted by a Servlet, but should be embedded as template in a JSP. Second, to achieve what you want, each button must sit in its own <form> element. Here's a kickoff example:

Servlet which loads the table data:

protectedvoiddoGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException) {
    List<Item> items = itemDAO.list();
    request.setAttribute("items", items);
    request.getRequestDispatcher("list.jsp").forward(request, response);
}

list.jsp which displays the table data:

<table><c:forEachitems="${items}"var="item"><tr><td>${item.someProperty}</td><td><formaction="servletUrl"method="post"><inputtype="hidden"name="id"value="${item.id}"><inputtype="submit"name="edit"value="edit"></form></td></tr></c:forEach></table>

Servlet which processes the edit:

protectedvoiddoPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException) {
    Longid= Long.valueOf(request.getParameter("id"));
    Itemitem= itemDAO.find(id);
    request.setAttribute("item", item);
    request.getRequestDispatcher("edit.jsp").forward(request, response);
}

No need for Javascript hacks to pass the row ID.

Post a Comment for "The Way To Get A Value From A Hidden Type Correctly"