Friends, I have a foreach in java that runs through a list to fill another and send it to my jquery, but I realized that the for is overwriting the objects.
Ex:
listingMethod (id) [0]) = 'samsung' and listingMethod (id) [0]) = 'iPhone 6'
On the first round l_jQuery [0] = 'Samsung' ,
in the second round l_jQuery [0] = 'iPhone 6' and l_jQuery 1 = 'iPhone 6' . I leave you some images and my code. Greetings and thanks.
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/plain");
HttpSession sesion = request.getSession(false);
PrintWriter out = response.getWriter();
int id_consumidor = Integer.parseInt(sesion.getAttribute("id_consumidor").toString());
Gson gson = new Gson();
Producto producto = new Producto();
List<Producto> lista = new ArrayList<>();
List<Producto> l = productoFacade.oferta_guardadas(id_consumidor);
productoFacade.oferta_guardadas(id_consumidor);
for (Producto p : l) {
producto.setNombreProducto(p.getNombreProducto());
producto.setIdProducto(p.getIdProducto());
producto.setPrecioNormal(p.getPrecioNormal());
producto.setPrecioOferta(p.getPrecioOferta());
producto.setRutaImagen(p.getRutaImagen());
lista.add(producto);
}
String json = gson.toJson(lista);
out.print(json);
}