What I want to do is to make a list of persons with id
and other attributes.
First of all the app opens with a form to enter personal data. Then it goes to the servlet responsible to capture the data. Then the servlet sends it to another JSP file that shows data. Then if the user chooses to add another one it adds it.
This worked fine but I had to change the app to add to the HTML table a submit button so the user can delete the person he wants using another servlet for delete.
I tried to set the ArrayList
that contains all the data as an attribute but it didn't work.
First i tried this way:
String x = request.getParameter("submit");
if (x != null) {
request.setAttribute("list", list);
request.setAttribute("id", p.getId());
request.getRequestDispatcher("Supprimer").forward(request, response);}
Then I tried this way:
out.println(" <tr><td> <form method='POST'>");
out.println("<input type='hidden' name='id' value='" + p.getId() + "' >");
request.setAttribute("list", list);
out.println("<input type='submit' value='Supprimer' name='submit' ></td></tr> ");
out.println(" <tr><td> </form >");
and this is the jsp file that will show the data
<%
ArrayList<personne> list = (ArrayList<personne>) request.getAttribute("list");
request.setAttribute("listt", list);
for (personne p : list) {
out.println("<table border='2'>");
out.println("<tr> <td>ID</td><td>");
out.println(p.getId());
out.println("</td> </tr><tr><td>Nom</td> <td>");
out.println(p.getNom());
out.println(" </td> </tr><tr> <td>Prenom</td> <td>");
out.println(p.getPrenom());
out.println(" </td> </tr><tr> <td>Sexe</td> <td>");
out.println(p.getSexe());
out.println(" </td></tr><tr><td>CodePostale</td><td>");
out.println(p.getCodePostal());
out.println(" </td></tr> </table> </br>");
out.println(" <tr><td> <form method='POST'>");
out.println("<input type='hidden' name='id' value='" + p.getId() + "' >");
request.setAttribute("list", list);
out.println("<input type='submit' value='Supprimer' name='submit' ></td></tr> ");
out.println(" <tr><td> </form >");
String x = request.getParameter("submit");
if (x != null) {
request.setAttribute("list", list);
request.setAttribute("id", p.getId());
request.getRequestDispatcher("Supprimer").forward(request, response);
}
out.println(" </table> </br>");
}
%>
this is the first servlet that will capture the data coming from the first submited form
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int id = Integer.parseInt(request.getParameter("id"));
String nom = request.getParameter("nom");
String prenom = request.getParameter("prenom");
String sexe = request.getParameter("sexe");
int codePostal = Integer.parseInt(request.getParameter("cd"));
personne p = new personne(id, nom, prenom, sexe, codePostal);
ap.add(p);
request.setAttribute("list", ap);
request.getRequestDispatcher("affichage.jsp").forward(request, response);
}
and this is the servlet responsibale for deleting the object and resend us to the first servlet so it can send us again to the jsp file to show us the new data
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ArrayList<personne> list = (ArrayList<personne>)request.getAttribute("list");
int id =Integer.parseInt(""+request.getAttribute("id"));
System.out.println(id);
java.util.Iterator<personne> itr = list.iterator();
while (itr.hasNext())
{
if(id==itr.next().getId()){
itr.remove();
}
}
request.setAttribute("list", list);
request.getRequestDispatcher("affichage.jsp").forward(request, response);
}
Btw this didn't help me: how to send ArrayList from jsp to servlet