How to get my array in the JSP page

0
public void Pesquisar(){
    try {
        Class.forName("com.mysql.jdbc.Driver");
    try {
    Connection conexao = DriverManager.getConnection("jdbc:mysql://localhost/CadastroProdutos", "root", "maquinarafa");
    Statement comando = conexao.createStatement();
    String comandoSQL = "select id_produto, nome, descricao, fornecedor,quantidade,tipo, link  from cadastro_produtos"; 
    ResultSet rsClientes= comando.executeQuery(comandoSQL);
    while(rsClientes.next()){
        System.out.println(rsClientes.getString("id_produto")+";"
                                +rsClientes.getString("nome")+";"
                                +rsClientes.getString("descricao")+";"
                                +rsClientes.getString("fornecedor")+";"
                                +rsClientes.getString("quantidade")+";"
                                +rsClientes.getString("tipo")+";"
                                +rsClientes.getString("link"));
        String idd = rsClientes.getString("id_produto");
        String name = rsClientes.getString("nome");
         String desc =rsClientes.getString("descricao");
         String forn = rsClientes.getString ("fornecedor");
         String qtde = rsClientes.getString ("quantidade");
         String tip=rsClientes.getString ("tipo");
         String caminhoimag=rsClientes.getString("link");
         //adicionando para o objeto 
         Produto p2 = new Produto();
         p2.setId(idd);
         p2.setNome(name);
         p2.setDescricao(desc);
         p2.setFornecedor(forn);
         p2.setQuantidade(qtde);
         p2.setTipo(tip);
         p2.setLink(caminhoimag);
         p2.arrayproduto.add(p2);
        }

///// I have a method that searches and returns meud data from mysql to the arrayproduct, but I want to access this array on a JSP page:

JSP PAGE

<%@page import="Classes.Produto"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Relatório de Produtos </title>
<h1 align = "center"> Relatório de Produtos </h1>
<link rel="stylesheet"
    href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script
    src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script><scriptsrc="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<%
Produto p = new Produto();
ArrayList<Produto> array =  ???? nao sei como receber este array na jsp
%>
    
asked by anonymous 10.11.2017 / 23:44

1 answer

0

Good evening Rafael, To access this collection in the JSP will depend on how you are making the JSP call.

If you are using the request.getRequestDispachter().forward(req,resp) method then you can access the list by adding it to a request attribute using the req.setAttribute("key", "value") method.

If you are using a resp.sendRedirect() da, you are a little more chatty. The options are not as feasible as the above method. The reason for this is that with the resp.sendRedirect() method the responsibility of the request for the client passes, and Servlet stops rendering this page. One of the alternatives is to use a session using the req.getSession().setAttribute("key", "value") method. (Just be sure that the session remains active during multiple requests, so you would have to clean it when necessary to avoid confusion.)

I assume you are using the first (most ideal) method. In your example, it would look something like this.

request.setAttribute("lista_produtos", p2.arrayproduto); .

And in your JSP, you would have access like this:

<% List array = (List) request.getAttribute("lista_produtos"); %>

I hope you have helped. Hugs.

    
14.11.2017 / 02:03