Pass and repass a variable from one JSP to another

When i try to pass a variable “valeur” from fiche1.jsp to fiche2.jsp it works and i can display the result of a query, but when i try to come back from fiche2.jsp to fiche1.jsp and pass to it the variable”valeur” so that i can execute a query, i get no result displayed in my jsp [ fiche1.jsp ]

I tried to test if i can display the value of the variable in fiche1.jsp, but i’m getting the value null.

fiche1.jsp

<body>
<%String val=(String)request.getAttribute("val");
                    System.out.println(val);
                    System.out.println("hayhay");
                 %>

                    <div class="onglets_html">
                         <div class="onglets">
                            <div class="onglet_y onglet"><a href="fiche1.jsp">Quoi</a></div>
                            <div class="onglet_n onglet"><a href="fiche2?valeur=<%=val%>">Qui</a></div>
                            <div class="onglet_n onglet"><a href="fiche3.jsp">Pourquoi</a></div>
                         </div>

                         <form method="get" action="fiche1">
                                <table border="1" cellpadding="3pt" align="center">
                                    <tr>
                                        <c:forEach var="acteur" items="${listActeur}">
                                    <tr>
                                        <td>salma</td>
                                        <td><c:out value="${acteur[0]}" /></td>
                                        <td><c:out value="${acteur[1]}" /></td>

                                    </tr>
                                        </c:forEach>
                                </table>
                            </form>
                    </body>
        </html>

Fiche1ControllerServlet

protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession(true);

        try {
            Session session1=HibernateUtil.getSessionFactory().getCurrentSession();
            String val=request.getParameter("valeur");
            session1.beginTransaction();
            List<Projet> list = 
                    session1.createQuery("select a.nomActeur, a.prenomActeur from Acteur a join a.acteurProjets p  where p.libelle= :libelle")
                            .setString("libelle", val)
                            .list();

            request.setAttribute("listActeur", list);
            request.setAttribute("val",val);
            request.getRequestDispatcher("fiche1.jsp").forward(request, response);
            session1.close();

               System.out.println(val);

           } catch (Exception e) {

               e.printStackTrace();
           }
        }

fiche2.jsp

<body>      
    <%String val=(String)request.getAttribute("val"); 

    %>

            <div class="onglet_n onglet"><a href="fiche1?valeur=<%=val%>">Quoi</a></div>
            <div class="onglet_y onglet"><a href="fiche2.jsp">Qui</a></div>
            <div class="onglet_n onglet"><a href="fiche3.jsp">Pourquoi</a></div>
        </div>

    <form method="get" action="fiche2">
                            <table border="1" cellpadding="3pt" align="center">
                                <tr>
                                    <c:forEach var="beneficiaire" items="${listBeneficiaire}">
                                <tr>
                                    <td>salma</td>
                                    <td><c:out value="${beneficiaire[0]}" /></td>
                                    <td><c:out value="${beneficiaire[1]}" /></td>
                                    <td><c:out value="${beneficiaire[2]}" /></td>
                                    <td><c:out value="${beneficiaire[3]}" /></td>
                                </tr>
                                    </c:forEach>
                            </table>
                        </form>
</body>
</html>

Fiche2ControllerServlet

protected void doGet(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {

        HttpSession session = request.getSession(true);

        try {
            Session session1=HibernateUtil.getSessionFactory().getCurrentSession();
            String val=request.getParameter("valeur");
            session1.beginTransaction();
            List<Projet> list = 
                    session1.createQuery("select b.nomBénéficiaire, b.prenomBénéficiaire, b.fonctionBénéficiaire, b.emailBénéficiaire from Bénéficiaire b join b.bénéficiaireProjets p  where p.libelle= :libelle")
                            .setString("libelle", val)
                            .list();

            request.setAttribute("listBeneficiaire", list);

            request.getRequestDispatcher("fiche2.jsp").forward(request, response);
            session1.close();

           } catch (Exception e) {

               e.printStackTrace();
           }

       }

Can anyone help me please?


Source: jsp

Leave a Reply