0

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 >");

Screenshot of the HTML output

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

M.W Sassi
  • 3
  • 7
  • I succeeded in filling and showing the persons object dynamically and i'm trying to edit or remove the person wich the user click delete or modify in it , and yes i'm using tomcat. – M.W Sassi Oct 17 '18 at 11:10

2 Answers2

0

The code you write can not work, because you are storing item info on request object when you are rendering the HTML page.

One solution is to define for each field an hidden HTML field, as you already define to id attribute.

xcesco
  • 4,690
  • 4
  • 34
  • 65
  • so there's no way i can keep passing an object through the diffrent jsp and servlette files ? coz i need another servlette for deleting the specific object from the arraylist – M.W Sassi Oct 17 '18 at 11:14
  • Every JSP and servlet is elaborated on the server side and then become an HTML page (on the client side). When you operate on an HTML page, you go from client to server. – xcesco Oct 17 '18 at 11:57
0

I tried to set the Arraylist that contains all the data as an attribute but it didn't work.

Form submit from jsp will eventually discard old request object itself since browser makes a new request to server.

Instead of relying on request.setAttribute("list", list) make use of session.setAttribute("list", list) just once in the servlet itself, no need to set in jsp.

Now, to access the list use session.getAttribute("list")