GeeCON Prague 2014*
The moose likes Servlets and the fly likes I Have Problem To Separate Business Logic From Front Controller Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "I Have Problem To Separate Business Logic From Front Controller" Watch "I Have Problem To Separate Business Logic From Front Controller" New topic
Author

I Have Problem To Separate Business Logic From Front Controller

JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
My program works. I raise this question NOT because something does not work.
Nonetheless, I have problem to separate business logic from front controller. Front controller; of course, is a servlet. Take a very simple example; say, authenticate a user against database when he/she tries to log in. I think the correct design is to have
1. a "business delegate" to take care of the validation.
2. a class that works on database access
3. a class that returns a connection object from the data source.
I always have my business delegate inside the front controller. I do not know how to spin business delegate off. I would like to have some guidance - what are supposed to be in my servlet, what are supposed in the business delegate, etc. Just want to show some code to illutrate what I mean.
In my servlet (front controller):

And, I put data access logic in the

The DBConnection.java in the ConnectionPool folder returns a connection object.
Ko Ko Naing
Ranch Hand

Joined: Jun 08, 2002
Posts: 3178
You might have known already, actually they have their different jobs. Front Controller should just act as a workflow management and they should be located between the client tier and presentation tier...
They actually control web components like JSPs and servlets
But for business delegate, they are just delegates(representatives) for business services... They act like proxy for web components in presentation tier... They are located between presentation tier and business tier...
They are actually used by web components like JSPs and servlets
I'm not sure whether my explanation is clear or not... Just something to make you understand about them...


Co-author of SCMAD Exam Guide, Author of JMADPlus
SCJP1.2, CCNA, SCWCD1.4, SCBCD1.3, SCMAD1.0, SCJA1.0, SCJP6.0
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8919

You would like to look at how struts works. It uses Action classes (or its Helper class)to encapsulate business logic. If you are using EJBs you will have your business logic there.


Groovy
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
I DO NOT gain any additioal knowledge from the replies provided by Ko Ko and Pradeep. Both do not answer my question directly.
I need help from someone who is willing to take a look at my code given and guide me what to put in my servlet, what to put in my business logic, and what to put in my data access logic.
kishore harindran
Greenhorn

Joined: Dec 04, 2002
Posts: 14
Hi,
I have gone through your code.
I would like to put something on this.
First , You use HTML/JSP page to display the page.
When the user submits form, you call your servltes.
Inside the servlets , you use your business class for doing your business logic method.
Instead of doing JDBC connection in your business class, u use another database class which will have only JDBC methods which is explained below.
You can further divide database class into two parts.
First part:
In this you define common methods used for database connections like opening connection,closing connection , returning resultset , transaction methods.This is because this class will be common for full project.
Second part :
In this , you write methods to get the result from database .This class should be initiated in your business class.
by doing the above in your code, you are following MVC structure.
in this ,
VIEW - JSP/HTML
CONTROLLER - SERVLETS
MODEL - BUSINESS CLASS
Hope this would clear your doubt.
regards
kishore
Axel Janssen
Ranch Hand

Joined: Jan 08, 2001
Posts: 2164
JiaPei: it might really be a good idea to focus a little on struts.
Because in those examples proper seperation of presentation/business logic and sometimes even database access-layers are explained.
With prior knowledge of servlets/jsp struts is not that difficult as some may think:
There are books/pdfs about struts available for around 20$ (Sue Spielmann, www.manning.com).
... and a quick starter by Rick Reumann: http://www.reumann.net/do/struts/main
for database access I would use http://java.sun.com/blueprints/patterns/DAO.html or (probably better) Hibernate.
In your code you have no separation between business logic and data access logic. Just take a very small business logic class (like that in Reumann tutorial) and DAO pattern as a start.
[ November 15, 2003: Message edited by: Axel Janssen ]
JiaPei Jen
Ranch Hand

Joined: Nov 19, 2000
Posts: 1309
Thanks a lot to Kishore and Axel for useful guidance; especially, two of you did not post messages for the purpose of winning a book.
 
GeeCON Prague 2014
 
subject: I Have Problem To Separate Business Logic From Front Controller