• Post Reply Bookmark Topic Watch Topic
  • New Topic

In MVC, who stores the business logic  RSS feed

 
Syed Khateeb
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,

I need some clarity on whether the Servlet(controller) or the JavaBean/reuasable java class(Model) holds the business logic code, invokes the EJB component, queries a database.

As i understand, its the servlet that handles the original request, invokes the business logic & data-access code and creates beans(model) to represent the results.
But i was confused when read in HFSJ, that business code is taken out of the servlet & put in model, which is combination of the business data & the methods that operate on the data.

Thanks.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The job of the servlet(controller) is to control the web flow of the application. The job of the JSP (view) control how the application looks.

That leaves the beans, the model.
In a small app your model beans may hold all of the business logic and/or make calls directly to the database.

In larger apps, they may be the part of the web app that communicates with EJBs or your data access objects.


The main thing is that the model beans should be able to operate in any environment, you should be able to call these bean's methods from a servlet, a swing app, the command line via main methods in the beans themselves, or from a testing framework. They should't require a servlet container to work.

Likewise, your view and controller should have no idea how the the model beans work.
The servlets and JSPs should have no idea that a bean is getting it's data by reading it from the local file system, by communicating with a relational database, web service, etc...
 
Cameron Wallace McKenzie
author and cow tipper
Saloon Keeper
Posts: 4968
1
Hibernate Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Control logic goes in the Servlet. Business logic should go in the JavaBean.

I always liken MVC to a home heating system. A Servlet is like the thermostat, taking input, validating it, and passing it to the model. The model would be the furnace, actually heating the house. The view would be the number of sweaters your sister is wearing.

Here's a pretty comprehensive tutorial on Model-View-Controller with regards to Servlets, JavaBeans and JSPs. Enjoy!

http://www.technicalfacilitation.com/get.php?link=whatismvc

-Cameron
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66144
144
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kameron McKenzie:
Business logic should go in the JavaBean.


I'd rather say that business logic goes into the business layer without imposing some structure, such as beans, onto it.

I use beans extesively as transfer objects, but the business layer APIs are generally not bean-patterend themselves.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!