How to display list of servlet in jsp?

0

Good evening. Guys, I have a question which after N attempts does not clear up, so I decided to ask the masters for help. Basically, I have the following servlet:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    System.out.println("Chamando doGet");

    PrintWriter resposta = resp.getWriter();
    resposta.println("<html><body>");
    resposta.println("<strong>TESTANDO FORTE</strong>");

    resposta.println("<ul>");
    for (Artista u : new ArtistaDAO().getArtistas()) {
        resposta.println("<li>" + u.getNome() + "</li>");
    }
    resposta.println("</ul>");


    resposta.println("</body></html>");

    RequestDispatcher disp = req.getRequestDispatcher("/WEB-INF/views/inicio.jsp");
    disp.forward(req, resp);
}

This servlet, (which has urlPatterns="/ get_artist") When accessed by / get_artist returns me a simple html list of registered artists. However, I would like to get this list on my JSP page (Dashboard) as soon as the user logs in. I know it's possible to use with taglib:

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    ...
    ...
    ...
<c:forEach var="item" items="${artistas}">
<ul>
<li>Nome: ${item.getNome}</li> 
</ul>
</c:forEach>

However, for this taglib to work, should I have another servlet that returns another list?

I'm trying to get through with the following servlet (but it's not working):

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    synchronized(this){
        ArrayList<Artista> artistas = new ArtistaDAO().getArtistas();

        req.setAttribute("artistas", artistas);

        RequestDispatcher disp = req.getRequestDispatcher("/WEB-INF/views/inicio.jsp");
    }
}

In short, my question is: How do I get all my ArrayList artists and display them on the dashboard as soon as a user logs in?

    
asked by anonymous 17.11.2018 / 00:08

1 answer

0

As far as I understand, when making the request it is possible to verify the action by which it was done by sending the form. In the example below, I made the option to send the different paths, if it is redirecting to the same location, you can remove the "action" option, and just return list. Here is the code below:

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
   String acao= req.getParameter("acao");
    if (acao!=null){
        switch (acao) {           
        case "listaArtista":
            String caminho = "/busca_artista.jsp";
            List<Artista> lista = artistaDAO.getArtistas();
            envioLista(req, resp, lista, caminho);
            break;           
        case "listaLogin":
            String caminho = "/login.jsp";
            List<Artista> lista = artistaDAO.getArtistas();
            envioLista(req, resp, lista, caminho);
            break;
        }
    }else{
        //Pode passar a lista aqui novamente ou alguma mensagem de validação
    }
}

In this function it sends the list and the path that is passed as a parameter:

private void envioLista(HttpServletRequest req, HttpServletResponse resp, List lista, String caminho)
        throws ServletException, IOException {
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(caminho);
    req.setAttribute("lista", lista);
    dispatcher.forward(req, resp);
} 

Example on the form

 <div class="row">
      <form action="/busca_artista" method="post" id="buscaArtista" role="form">       
        <input type="text" id="acao" name="acao" value="listaArtista"> <!-- esse campo tem que ser hidden na hora do envio-->
        <!-- o restante do código ... -->
      </form>
</div>
    
14.01.2019 / 13:15