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.

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
2  
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 '14 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 '14 at 18:50

1 Answer 1

you can use ORM (object relational mapping) like JPA or Hibernate. when you are using ORM in your model, you dont need to re-query the related object because ORM will handle it for you automatically. for example:

define your model class and it's relationship:

class Product
{
    Manufacturer manufacturer;

    //setter and getter
}

class Manufacturer
{
    Long id;
    String name;

    //setter and getter
}

calling the model in the controller's method:

public Result showProductList()
{
   List<Product> products = ProductModel.get();
   return Result.ok().render("products", products);
}

and you can call the manufacture name in product in from your product model.

<c:forEach var="product" items="${products}">
    <option value ="10"><c:out value="${product.manufacturer.name}"/></option>
</c:forEach>

disclaimer: the example code above is not the real code. I just want to show the big picture about using the ORM in your code. using this approach you won't break the MVC design pattern (because you don't need to re-query the object from your view)

share|improve this answer

Your Answer

 
discard

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.