pass data to a jsp from a java class

0

How can I pass data from a query to a jsp? Let me explain:

I have a jsp page from which I get parameters, I pass them to a servlet, this in turn executes a query class that goes to the bd, but I do not know how to pass the result of that query to the same jsp.

    
asked by Jeferson Martinez 01.08.2016 в 03:31
source

3 answers

2

If you want to send data from the servlet to the view, you can use forward from the servlet to your JSP and add all the information you need to show as attributes of the request, then in the JSP you can access these attributes without problems.

Here is an example:

@WebServlet("busqueda.jsp")
public class BusquedaServlet extends HttpServlet {

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

        //realizas la búsqueda aquí...
        List<Resultado> resultados = ... //lista con los resultados de la búsqueda
        //colocamos los resultados de la búsqueda como atributo del request
        request.setAttribute("resultados", resultados);
        //realizamos un forward a la página JSP donde mostraremos los resultados
        request.getRequestDispatcher("busqueda.jsp").forward(request, response);
    }
}

JSP, the file searched.jsp (I use JSTL, recommended to avoid using scriptlets in your JSP code):

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>Resultado de búsqueda</title>
    </head>
    <body>
        <table>
            <tr>
                <th>Id</th>
                <th>Descripción</th>
            </tr>
        <c:forEach items="${resultados}" var="resultado">
            <tr>
                <td>${resultado.id}</td>
                <td>${resultado.descripcion}</td>
            </tr>
        </c:forEach>
        </table>
    </body>
</html>

It is assumed that the class Resultado has the following structure:

public class Resultado {
    private int id;
    private String descripcion;

    //getters y setters para los campos de arriba
}
    
answered by 01.08.2016 в 16:54
0

If you do not have to submit the form yet (which you can do with a submit + request POST button), you can make an ajax call that communicates with your server without having to reload the entire page.

$.get( "/ruta/metodoMapeado", function() {
  alert( "llamando al host" );
}).done(function(data) {
    alert("la respuesta es: " + data);
}).fail(function(error) {
    alert("hemos tenido el error" + error);
});
    
answered by 01.08.2016 в 09:36
0

Add a map object to the response with the result object you want to query in the jsp

Map<String, Object> model = new HashMap<String, Object>();
model.put("objeto", ObjetoQueQuieresConsultarEnJSP);
return new ModelAndView(home, "model", model);

Then consult the jsp in the following way:

<% Map model = (Map) request.getAttribute("model"); %>

<% Object objecto = model.ObjetoQueQuieresConsultarEnJSP %>

In the object variable you already have the object that you want to consult in JSP and you can work with it normally

    
answered by 01.08.2016 в 11:40