简体   繁体   中英

Send object from a JSP page

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

HTML输出的屏幕截图

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM