Doubts with Servlets and JSP


I am developing a web page where on the home page (in my case it is my "Home" servlet) I wish to present information from several tables as well as queries to them. What I have achieved is to obtain the information of my company / brands table and show the last companies added, the detail is that I do not know how to send more than one attribute by the request.setAttribute () as I wish show apart from the information of my table companies also I want to show information in my table users and show my users as well as my table news show the last post (news from suppliers).

I look forward to your help, since I am super stuck with this.

This is my code for my servlet.

public class InicioController extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        RequestDispatcher rd;
        //mi conexión a mi base de datos
        conexion conn = new conexion();
        //le mando el objeto de mi conexión al constructor de mi claseDAO
        EmpresaDAO fun = new EmpresaDAO(conn);
        List<empresa> lista = new LinkedList<>();
        //realizo la llamada al método que obtiene la información de mi tabla empresas
        //Aqui es donde quiero mandar mas de un atributo a mi pagina index
        request.setAttribute("empresas", lista);
        rd = request.getRequestDispatcher("/index.jsp");
        rd.forward(request, response);
asked by user3084314 26.11.2018 в 21:00

1 answer


When you set an attribute to the request, what you do is add to the request an object associated with a key, which is the first value of the method.

The code:

request.setAttribute("empresas", lista);

Set the indicated list for the "companies" attribute.

If you want to send another item you just have to associate it with a different attribute name.


request.setAttribute("empresas", listaEmpresas);
request.setAttribute("usuarios", listaUsuarios);
request.setAttribute("usuariosDestacados", listaUsuariosDestacados);

And when retrieving these elements can be done by the name of the key:

request.getAttribute ("companies");

answered by 27.11.2018 в 11:30