简体   繁体   中英

How to send two different ArrayList from a Servlet to a JSP

I'll try to be clear in a few lines, showing only the essential code. As I said in the title, I have to send two arraylists from a servlet to a JSP page. Unfortunately, I don't know how to send multiple arraylists to a JSP, but I know how to send one only. I usually use this procedure:

example

ArrayList<ClassA> array_A = new ArrayList<ClassA>();

// [...] After some operations and have filled the array_A with objects of class ClassA

ServletContext sc = request.getSession().getServletContext();
request.setAttribute("Attribute", array_A);
RequestDispatcher rd = sc.getRequestDispatcher("/MyJSP.jsp");
rd.forward(request,response);
request.getSession().removeAttribute("Attribute");

This procedure allows me to send one arraylist to a JSP page. How can I send two different arraylist? For example, I need to send these two arraylist:

ArrayList<ClassA> array_A = new ArrayList<ClassA>();
ArrayList<ClassB> array_B = new ArrayList<ClassB>();

// [...] After some operations and have filled array_A with objects of class ClassA 
// & array_B with objects of class ClassB

How can I send these two arrays from the servlet to the JSP page? Could you kindly share me the code? I'm sorry if I wasn't so rigorous.

request.setAttribute("array_A", array_A);
request.setAttribute("array_B", array_B);

within jsp (when using jstl core library, with prefix c :

<c:forEach var="itemA" items="${array_A}">
      <!-- some code here -->
</c:forEach>

and

<c:forEach var="itemB" items="${array_B}">
      <!-- some code here -->
</c:forEach>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM