How to view Servlet’s list in jsp?

Asked

Viewed 188 times

0

Good evening. People, I have a question that after N attempts is not clarified, 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 the urlPatterns = "/artist search") When accessed by /artist search returns me a simple list in html of the registered artists. However, I would like to get this list on my JSP (Dashboard) page as soon as the user logs in. I know it is 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 I must 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");
    }
}

To sum up my question: How to get all my Arraylist artists and show on Dashboard as soon as a user logs in?

1 answer

0

From what I understand, when making the request it is possible to verify the action by which it was made by sending the form. In the example below, I made the option to send different paths, if it is redirecting to the same location, you can remove the "action" option, and just return list. Follow 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 you send the list and the path that are passed as 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>

Browser other questions tagged

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