How to receive my array on the JSP page

Asked

Viewed 248 times

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 mysql data for the arrayproduto , however I want to access this array in 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>
<script
    src="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
%>

1 answer

0

Good evening Rafael, To get access to this Collection in JSP will depend on how you are making the JSP pro call.

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

If you are using a resp.sendRedirect() then it gets a little more boring. The options are not as viable as the method above. The reason is that with the method resp.sendRedirect() passes the responsibility of the request to the client, and Servlet ceases to render this page. One of the alternatives is to use a session through the method req.getSession().setAttribute("key", "value") (Just don’t forget that the session remains active during multiple requests. Then you would have to clean up when necessary to avoid confusion.

I suppose you’re 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’d have access like this:

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

I hope it helped. Hugs.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.