File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes single or multiple servlets Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "single or multiple servlets" Watch "single or multiple servlets" New topic

single or multiple servlets

nelson christos
Ranch Hand

Joined: Aug 08, 2006
Posts: 57
in our application the user enters the master data online.
right now the buisness logic is implemented in a servlet.
but the servlet has grown too long. will it be a better idea
if i create some more servlets, will this help.


i think therefore i am
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 10031

Is that servlet going to process the business logic?? or is it just the controller servlet?? If that is a controller servlet then I guess just a single instance would be more than enough for a medium sized app. In our project we have a single controller servlet.

SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
Shantanu Puranik
Ranch Hand

Joined: Jun 10, 2004
Posts: 67
It's prefferd to have the business logic into simple java classes rather than writing into servlet.
This is for code reuse. Coders can reuse the same class , if they require to implement the same logic in other applications in future
e.g. Stand alone application (Swings).

In your application, user will interact with the master data. I guess it is simple Add/Update/Delete kind of functionality with/without some more business logic.
In my last project, what I did is, separated Add/update/Delete functionality into one class and additional business logic into other class.
Now from servlet, call the methods of these classes.
This will cut the length of code in servlet hence, easier to maintain.
[ April 09, 2007: Message edited by: shantanu puranik ]

--- Shantanu
nelson christos
Ranch Hand

Joined: Aug 08, 2006
Posts: 57
thanks for the reply shantanu
its exactly the same process that you mentioned
and we are doing those add/update/delete in some
helper classes.
but the list of menu items for master data is very long.
my query was whether for each menu create a "servlet" or
go for a single servlet for all master data(which is what we are doin).
does a large servlet pose performance issues
Shantanu Puranik
Ranch Hand

Joined: Jun 10, 2004
Posts: 67
Writing a servlet for each link will be duplication of code.
and following the existing one, i.e. single servlet could be maintenance nightmare.
In addition, in single servlet there will be lot of if.. else if..
statements which will hamper the performance since every condition will be checked untill the match is found.

A midway could be, grouping of links and have a servlet for each group.
This grouping could be on functional similarity or if you have different modules in the project, a servlet for each module.
Needless to say, provide mapping for each link request to the perticular group servlet in web.xml

There could be design pattern available for the said problem. but my inexperience is stopping me from suggesting one.

Expert ranchers please comment.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Looks like not using any typical frameworks.
You can do mapping stuff using HashMap in your servlet or in a seperate class just instantiate that class in your servlet init(). Your class should create and fill the map upon its instantiation.
Ben Souther

Joined: Dec 11, 2004
Posts: 13410

Read up on the "Front Controller" or command pattern.
This pattern allows you to have one controller servlet that can serve up an unlimited number of command objects.

This article by Bear Bibeault may help:

Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
I agree. Here's the link:
subject: single or multiple servlets
It's not a secret anymore!