Tzu ng
Tzu ng

Reputation: 9244

Java Servlet - Use one returned object for multiple purposes in view

Let me clarify my context:

In my index page : I have featured products section, products section and categories which contain products categorized. I use my controller to return a list of products to view. So I think about a solution:

I will pass: 3 different lists of products. So my view is clean with pure EL, no scriplet. But I have to forward a big packets through network which is not efficient.

So are there any other ways to solve this problem ? Thanks first

Upvotes: 0

Views: 67

Answers (2)

DwB
DwB

Reputation: 38320

One object can have 3 lists as data members. For example:

class IndexViewModel
{
  private List<String> listOne;
  private List<Blam> listTwo;
  private List<Integer> listThree;

  public List<String> getListOne()
  {
    return listOne;
  }

  ... the rest.
}

Then in your handler:

IndexViewModel indexViewModel;
... fill in the lists
request.addAttribute("BlammyTram", indexViewModel)

In your JSP:

<c:foreach items="${Blammytram.listOne}">
 ...
</c:foreach>

Upvotes: 0

BalusC
BalusC

Reputation: 1109132

There's some confusion going on. Those 3 lists are not sent over network. Only the JSP-generated HTML output is been sent over network. Java/JSP/Servlet runs in webserver, not in webbrowser.

If your concrete problem is that those lists are fairly large (e.g, more than 25 rows), then you should consider DB-level pagination. Or, if your concrete problem is that the JSP-generated HTML response is fairly large, then you should consider GZIP compression, it can save up to 80% of network bandwidth. Check the server manual how to enable it. In case of for example Tomcat, it's a matter of adding compression="on" to the HTTP <Connector> element in /conf/server.xml.

Upvotes: 1

Related Questions