1

I am using a form nested inside a table row to generate a remove button for each row, the action of the form is a servlet call which will then call the remove method in a java class.

How do I add the "id" value to each button so it will send the correct car object to the method to be removed from my database.

My JSP:

<h4>Current Cars Listed</h4>
<input type="button" value="Add Car" onclick="window.location='AddCar.jsp'">
<% 
List<Car> resultList = new ArrayList<Car>();
resultList=(List<Car>)request.getAttribute("ResultList");

%>
<table border="1">
<thead title="Current Cars"/>
<tr><th>Make:</th><th>Model:</th><th>Year:</th><th>Colour:</th><th>Information:</th></tr>
<% for(int i=0; i<resultList.size(); i++){%>

<tr><td><%=resultList.get(i).getCarMake()%></td><td><%=resultList.get(i).getModel()%></td><td><%=resultList.get(i).getCarYear()%></td>
<td><%=resultList.get(i).getCarColour()%></td><td><%=resultList.get(i).getInformation()%></td>
<td><form  action="CarServlet" method="get" ><input type="submit" value="Remove" name="remove"></form></td></tr>
<% }%>


</table>
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
SteveK
  • 207
  • 2
  • 6
  • 17

1 Answers1

3

You may add a hidden value inside the form to add the id:

<td>
    <form action="CarServlet" method="get">
        <input type="hidden" name="carId" value="<%= resultList.get(i).getId() %>" />
        <input type="submit" value="Remove" name="remove">
    </form>
</td>

Since you're already using request attribute, it would be better to stop using scriptlets at all and use Expression Language + JSTL.

<table>
<thead>
    <!-- current thead -->
</thead>
<tbody>
<c:forEach items="${ResultList}" var="car">
    <tr>
        <td>${car.carMake}</td>
        <td>${car.model}</td>
        <td>${car.carYear}</td>
        <td>${car.carColour}</td>
        <td>${car.information}</td>
        <td>
            <form action="CarServlet" method="get">
                <input type="hidden" name="carId" value="${car.id}" />
                <input type="submit" value="Remove" name="remove">
            </form>
        </td>
    </tr>
</c:forEach>
</tbody>

See how code above is better for readability and maintainability compared to your original code that uses scriptlets.

Community
  • 1
  • 1
Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • thanks that is correct and exactly what i was looking for but how do I then pull that value out in the servlet? I tried "String carID=request.getParameter("carId");" but it just gives a null value – SteveK May 11 '14 at 23:31
  • @SteveK it should work with `request.getParameter("carId");`. Are you sure you're using it in the `doGet` method of your `CarServlet`? – Luiggi Mendoza May 11 '14 at 23:32
  • I Actually used JSTL in the same jsp already can't believe I didn't think to use it again, yes that is much neater. I am sending it to the get method still returning null – SteveK May 11 '14 at 23:45
  • 1
    @SteveK my bad. Updated the answer. That should work. – Luiggi Mendoza May 11 '14 at 23:54
  • Thanks for your help,everything works with JSTL except the id which I was going to use in the method to remove this particular entry from database.The CarID comes through using resultList.get(2).getId() method fine but this will only work with a for loop. the JSTL method works fine for every other database field(carMake, carModel, etc.) but it will not pull out the int value for cars_id. I have tried to change it to every other possible version I can see in my Car.java mapping file and it keeps giving me an error of "Property 'cars_id' not found on type logon.Car" Any Ideas? – SteveK May 12 '14 at 17:29