Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How to separate Business Logic from the servlet

Posts: 18
Tomcat Server Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:


and i put UserName & Password in database name DB2

can anyone tell me the code how to do it...

Posts: 4568
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
Author and all-around good cowpoke
Posts: 13078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 .

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

Best regards
today's feeble attempt to support the empire
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
    Bookmark Topic Watch Topic
  • New Topic