Two Laptop Bag*
The moose likes Servlets and the fly likes How to separate Business Logic from the servlet Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "How to separate Business Logic from the servlet" Watch "How to separate Business Logic from the servlet" New topic
Author

How to separate Business Logic from the servlet

Shanu Pandey
Greenhorn

Joined: Mar 17, 2011
Posts: 18

I have made a simple login example which checks Userid & Password from a Microsoft Access Database...I put all the code in one servlet ...But now i want to put my Business Logic in a seperate java class....But i am unable to do it..

The code written by me is here:
This is the servlet:




and the two html pages made by me are:
(1)index.html




(2)M.html


and i put UserName & Password in database name DB2

BUT NOW I WANT TO PUT BUSINESS LOGIC IN A SEPERATE JAVA CLASS...
can anyone tell me the code how to do it...

Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4369
    
    8

Hi Shanu. Welcome to The Ranch! Your posts are much easier to read (and so more likely to get help) if you UseCodeTags. I've added them for you this time.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12769
    
    5
So write a business logic class that does not depend on the servlet environment. You can generalize the input to this class so that it can be tested as a standalone application,.

When you are sure it works right, your servlet can create an instance and use it - the "helper class" pattern.

For example, all of the POST input in the request can be gotten as a Map - write your helper class to use a Map as input.

See the javax.servlet.ServletRequest getParameterMap() method.

You can write your helper class with a main method so that it can be executed directly.

Bill
Lino Larios
Greenhorn

Joined: Jun 27, 2008
Posts: 28
Hi

Check the MVC pattern , so you could invoke the methods of a third class from you servlet passing as the parametters the values of your request .

http://www.javaworld.com/javaworld/jw-12-1999/jw-12-ssj-jspmvc.html

Also you could code a class just to create the db connection and the db operations

Best regards
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to separate Business Logic from the servlet