-1

I have to send three ArrayList from the servlet to jsp. My problem is i cannot return three arrayList from the same method.

public ArrayList afficher(String s)
ArrayList <A> list = new ArrayList<A>();
ArrayList <B> list2 = new ArrayList<B>();
//some operations

list.add(new A("aaa"));

list2.add(new B("bbbb"));

return list, list2;





ow can I proceed?


2 Answers2

1

You can store variables as attributes in ServletContext, and retrieve them in your JSP. See this previous question for details.

JustAnotherDeveloper
  • 2,061
  • 2
  • 10
  • 24
-1

You can create your method with ModelAndView as return type.

ModelAndView mav = new ModelAndView();
mav.setViewName("welcomePage");
mav.addObject("list", list);
mav.addObject("list2", list2);
return mav

In that mav object you can add n number of models.

And on jsp you can access them using ${list}, ${list2}.

@Data
@Builder
class Main {

    public List<List<Object>> getList() {
        List<List<Object>> list = new ArrayList<>();
        list.add(Arrays.asList(new A[]{new A("A"), new A("A1"), new A("A2")}));
        list.add(Arrays.asList(new B[]{new B("B"), new B("B1"), new B("B2")}));
        return list;
    }

}

class A {
    String name;

    public A(String name) {
        this.name = name;
    }
}

class B {
    String name;

    public B(String name) {
        this.name = name;
    }
}
Sagar Gangwal
  • 7,544
  • 3
  • 24
  • 38