Pass variable from JSP to SERVLET using AJAX

0

I have a list of orders and when I click on one I want to open a new page with the details of that order. I was reading and it can be done with ajax but all I saw were with form but I want you to click on the div.

Orders.jsp

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Pedidos</title>
        <link href="css/menu.css" rel="stylesheet" type="text/css"/>
        <link href="css/bootstrap.css" rel="stylesheet" type="text/css"/>
        <link href="css/listaPedidos.css" rel="stylesheet" type="text/css"/>
        <link href="https://fonts.googleapis.com/css?family=Roboto:500" rel="stylesheet">
        <script src="js/jquery.js" type="text/javascript"></script>
        <script>
            function verDetalles(id){
                alert(id);
                $.ajax({
                    url: 'DetallesServlet',
                    dataType: 'json',
                    data: { miPostVar: id },
                    type: 'post'
                });
            }
        </script>
    </head>
    <body>
        <nav class="tab">
            <ul class="topnav">
                <li><a class="noactive" href="Actividad.jsp">Actividad</a></li>
                <li><a class="active" href="ConsultaPedidosServlet">Pedidos</a></li> 
            </ul>
        </nav>

        <c:forEach items="${repartos}" var="reparto">
            <div id="${reparto.pedido.id}" class="listaPedidos" onclick="verDetalles(this.id)">
                <p> #${reparto.pedido.id} </p>
                <p> ${reparto.pedido.descripcion} </p>
                <p> ${reparto.pedido.direccion} </p>
            </div>
        </c:forEach>

    </body>
</html>

DetailsServlet.java

@WebServlet(name = "DetallesServlet", urlPatterns = {"/DetallesServlet"})
public class DetallesServlet extends HttpServlet {

    private String id = null;

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

        Consulta cons;
        try {
            cons = new Consulta("*", "pedidos");
            ResultSet rs = cons.recuperarPorId(Integer.parseInt(id));
            Pedido ped = null;
            if (rs.next()){
                ped = new Pedido(rs.getInt("id"), rs.getString("descripcion"), rs.getInt("precio"), rs.getString("direccion"), rs.getString("fechaHora"), rs.getString("comentario"), rs.getString("estado"));
            }

            request.setAttribute("pedido", ped);
                request.getRequestDispatcher("/DetallesPedido.jsp").forward(request, response);

        } catch (SQLException | ClassNotFoundException ex) {
            Logger.getLogger(ConsultaPedidosServlet.class.getName()).log(Level.SEVERE, null, ex.getMessage());
        } 

    }


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

            id = request.getParameter("miPostVar");
            doGet(request, response);

    }
}
    
asked by Juan Manuel 08.11.2016 в 04:50
source

1 answer

1

If you want to load in another page it is not necessary the ajax you can use a link (a) sending the id of the order to redirect it to another page an example:

Pedidos.jsp

<c:forEach items="${repartos}" var="reparto">
    <div id="${reparto.pedido.id}" class="listaPedidos"
           onclick="verDetalles(this.id)">
        <p> #${reparto.pedido.id} </p>
        <p> ${reparto.pedido.descripcion} </p>
        <p> ${reparto.pedido.direccion} </p>
        <p> <a href="/DetallesServlet?id=${reparto.pedido.id}" /> </p>
    </div>
</c:forEach>

And in the DetallesServlet.java you get the id and you make the query:

protected void doGet(...) {
        String id = request.getParameter("id");Consulta cons;
    try {
        cons = new Consulta("*", "pedidos");
        ResultSet rs = cons.recuperarPorId(Integer.parseInt(id));
        Pedido ped = null;
        if (rs.next()){
            ped = new Pedido(rs.getInt("id"), rs.getString("descripcion"), rs.getInt("precio"), rs.getString("direccion"), rs.getString("fechaHora"), rs.getString("comentario"), rs.getString("estado"));
        }

        request.setAttribute("pedido", ped);
            request.getRequestDispatcher("/DetallesPedido.jsp").forward(request, response);

    } catch (SQLException | ClassNotFoundException ex) {
        Logger.getLogger(ConsultaPedidosServlet.class.getName()).log(Level.SEVERE, null, ex.getMessage());
    } 
    }

And in the jsp DetallePedido you show it.

    
answered by 08.11.2016 / 16:23
source