How to redirect to another jsp page without reloading?

0

I have 3 files jsp Registrar_Desayuno.jsp , add_desayuno.jsp and save_desayuno.jsp , the first jsp ( Registrar_Desayuno.jsp ) is the registration form, the second validates if the code is correct, if it is correct it sends me to the third jsp ( save_desayuno.jsp ) that is in charge of redirecting again to the first jsp ( Registrar_Desayuno.jsp ) that is the form.

What I want to do is that when I register breakfast I do not recharge the page. How can I do it?

  

Register_Breakfast.jsp

 <body onload="acceso.dni.focus()">
    <%
        String nombres = request.getParameter("Nombres");
    %>
    <form id="acceso" name="acceso" action="add_desayuno.jsp" method="post">
        <table style="width: 100%!important; height: 200px;">
            <tr align="center">
                <th class=titulo colspan="2"><h2><p class="text-center"><b>REGISTRO DE DESAYUNOS</b></p></h2></th>
            </tr>
            <tr align="center">
                <td>
                    <%if (nombres == null) {%>
                    <h2 style="color: #28B463;"></h2>
                    <%} else {%>
                    <script type="text/javascript">
                        $(document).ready(function () {
                            setTimeout(function () {
                                //$('.box').css('color', '#ffffff', 'background', '#ffffff'); //pinta
                                $(".box").fadeOut(1500); //oculta
                            }, 2000);
                        });
                    </script>

                    <h3 style="color: #4d8a43;" class="box caja"><b><%=nombres%></b></h3>

                    <%}%>
                </td>
            </tr>
            <tr align="center">
                <td>
                    <h3>Ingrese su Codigo:</h3>
                </td>
            </tr>
            <tr>
                <td align="center">
                    <input style=" padding: 2%; width: 20%; text-align: center; font-size: 200%;" class="form-control" type="text" name="dni" id="dni" size="400" maxlength="8" value="" required><br>
                </td>

            </tr>

            <tr align="center" >
                <td align="center" colspan="2" >
                    <br>
                    <input style="display : none;" class="btn btn-primary"  type="submit" name="continuar" value="continuar">
                </td>
            </tr>
        </table>
    </form>
    <br>


</body>
  

add_desayuno.jsp

  <body>
    <%
        String s_dni = request.getParameter("dni");
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/bd_pension",
                "root", "");
        Statement st = con.createStatement();
        ResultSet rs;

        int idpersona = 0;
        int idpensionista = 0;
        String nombres = "";
        rs = st.executeQuery("SELECT p.idPERSONA, pe.idPENSIONISTA, CONCAT(p.Nombres, ' ', p.Apellidos) as Nombres,"
                + " p.dni FROM persona p, pensionista pe "
                + "WHERE p.idPERSONA = pe.idPERSONA "
                + "AND p.dni='" + s_dni + "' "
                + "AND DATE_FORMAT(pe.fecha_ingreso,'%Y%m') = DATE_FORMAT(sysdate(), '%Y%m') ");
        if (rs.next()) {

            idpersona = rs.getInt("idpersona");
            idpensionista = rs.getInt("idpensionista");
            nombres = rs.getString("Nombres");

            session.setAttribute("s_dni", s_dni);
            response.sendRedirect("save_desayuno.jsp?idpersona=" + idpersona + "&idpensionista=" + idpensionista + "&Nombres=" + nombres);

        } else {
            response.sendRedirect("../../../falla.jsp");
        }
    %>

</body>
  

save_desayuno.jsp

<body>

    <%
        en emergencia

        String idpersona = request.getParameter("idpersona");
        String idpensionista = request.getParameter("idpensionista");
        String nombres = request.getParameter("Nombres");

        Class.forName("com.mysql.jdbc.Driver");
        Connection conexion = DriverManager.getConnection("jdbc:mysql://localhost/bd_pension", "root", "");

        Statement Estamento = conexion.createStatement();
        int rs = Estamento.executeUpdate("INSERT INTO desayuno (idDESAYUNO, idPENSIONISTA, titulo, estado, fecha_d, monto, cantidad) VALUES "
                + "(NULL, '" + idpensionista + "', 'DESAYUNO', '1', sysdate(), '3', '1');");

        response.sendRedirect("Registrar_Desayuno.jsp?Nombres=" + nombres);


    %>

</body>
    
asked by Andy Giampierre Ordoñez Vega 12.07.2018 в 23:45
source

1 answer

0

What you can do is forward the request something like this:

protected void doGet(HttpServletRequest request, HttpServletResponse response){

      /*
      Por acá estaría toda tu lógica de negocio
      */

      //Por último le seteas a tu objeto request, los atributos que serán 
      //recibidos en el JSP que se reenvie
      request.setAttribute("nombreAtributo", objeto);
      request.getRequestDispatcher("ruta del jsp").forward(request, response);
}

In the JSP you already get it with the method request.getAttribute("nombreAtributo") to put an example in the JSP, it would be something like this:

<% 
     Categoria categoria = (Categoria) request.getAttribute("nombreAtributo");
%>

And only the resend was made, so your page will not be reloaded and the url will not change.  I hope I've helped. Greetings.

    
answered by 19.07.2018 в 22:32