I'm following this tutorial: http://wiki4.caucho.com/Building_a_simple_listing_in_JSP It creates a basic web application in Eclipse using the MVC pattern with Resin as the web container. Here's the part I don't understand.

For the model, it creates a POJO, Interface and Interface Implementation. The data represents a Book. When we get to the controller, it creates a simple servlet. It calls the interface instead of the interface implementation. How does the interface to execute the implementation class? Also what if there were two impl classes, how would the interface know which to call?

1 Answer 1


The controller has a variable that can have a reference to any object of the interface type. The interface implementation class you speak of implements the interface, therefore it is a type of that interface.

This means that variable can have a reference to an object of that class.

This tutorial utilizes CDI which is something called a Dependency Injection framework. This allows you to configure beans in your application that automatically are instantiated and assigned to properties (variables like the one of type interface) for use by other beans. It is creating the object of the interface implementation class and adding it to your controller for you.

  • @Johnny_v just some explanation why a variable of the interface is a good idea here. It means you can more easily write a new implementation class and reconfigure it to be injected into your controller. This wouldn't require you to rewrite code in your controller. This concept is called loose coupling.
    – maple_shaft
    Commented Jun 27, 2015 at 21:35
  • Follow up, do I have to use the CDI? Can I manually create the object of the implementation class instead of using the Dependency Injection Framework?
    – Johnny_v
    Commented Jun 27, 2015 at 21:40
  • @Johnny_v sure you can, you can do whatever you want
    – maple_shaft
    Commented Jun 27, 2015 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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