Talking about MVC with servlets and JSP's, in a context about products and manufacturers , when querying the application for a list of products:
- the servlet loads a list of products from the database in a
Listobject (or similar);
- the servlet sets that list as attribute of the request and forwards the request to a JSP;
- the JSP retrieves the list from the request and displays its contents on the webpage.
If the above is correct, my question is: how do I load products manufacturers (let's suppose that each product contains the id of its manufacturers)?
A common approach I saw around is that when the JSP loops inside the list object, upon showing each product is calls some other class method which finally runs a query on the database.
There are two things that I dislike about this approach:
- it breaks the MVC pattern;
- several, usually separate, queries are run on the database, potentially loading the same data (i.e. when two products come from the same manufacturer).
Is the above true? Are there better approaches that solve the problem?
So far I'm thinking about making a servlet load the manufacturers data in a separate object (e.g. a
Map) along with the products
List: both objects are set to the request, and then the JSP can access both when rendering the page.
A (potentially dangerous) drawback of this idea is that it requires more memory to hold all the informations; anything else?