Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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 List object (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 oject, 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:

  1. it breaks the MVC pattern;
  2. 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?

share|improve this question
1  
If you're going to use JSP, you should probably use it on its own terms. You are already describing a level of detail that you have to attend to that is much higher than, say, ASP.NET MVC and C#. In other MVC systems (including ASP.NET MVC), you would be describing a ViewModel. –  Robert Harvey Sep 15 at 18:42
    
What you are proposing is just a plain old cache of manufacturers. More than the memory, a consideration would be if it is worth the effort (and if it is, maybe the cache should lay inside the application logic, not in top of it). Generally I would say that it does not, because for getting the details of a product you typically will spend a very little time (and quite constant), so the user will not have an issue with the extra database access (if the server has capacity issues, get a better HW or provide a better design). –  SJuan76 Sep 15 at 18:50

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.