aspose file tools*
The moose likes Servlets and the fly likes Web app design confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Web app design confusion " Watch "Web app design confusion " New topic
Author

Web app design confusion

Dhaval J. Patel
Ranch Hand

Joined: Mar 10, 2011
Posts: 90

Hello everyone,

I am facing a complicated design issue. I would like to have your opinions on what approach should I take.

I am dealing with the code written by previous programmers. It is a small booking web application.

The web application has a single servlet with a big if-else block (with around 80 conditions) where it compares one of the parameter and decides where to redirect the request and what processing to do. The same servlet has around 55 functions which are being called by different parts of the if-else block. These functions connect directly to the data access objects. There are a few javabeans which are used by the functions and the data access objects. The data access objects contain business logic and the data access code. Everything tangled together.

My boss wants me to add more functions to this web app. I tried adding stuff to it but it breaks other things. So I started cleaning up and cleaned up the data access objects and removed business logic from them. In short added an extra layer before the data access objects. But I havent integrated it with the main application yet. And now I am trying to remove all the code from the servlet and form a business layer with those functions in different classes.

But the servlet still has that bigg if-else block. Should I make one servlet for each condition or group them based on commonality. I was wondering how much code should every servlet have? I need to get request parameters put them into session variables, call appropriate business methods and redirect to appropriate page. Even that is a lot of code. I was wondering how much code should an ideal servlet have and how to ideally decide on the number of servlets required ?

And this system is running right now. So I cannot even do an overhaul and implement the web app with a framework. It has to be gradual and I need to add functions to the web app too. It is a nightmare. Please give me some suggestions on how to approach this situation. I am a newbie and totally new to such situations.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Each servlet should have one and only one purpose. The second that you have mutually exclusive if-else's or switch labels, your code is an anti-patterm.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Dhaval J. Patel
Ranch Hand

Joined: Mar 10, 2011
Posts: 90

Bear Bibeault wrote:Each servlet should have one and only one purpose.


So, should I make a servlet for each condition. Wouldnt that make a large number of servlets ?
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61187
    
  66

Yes. But the number of servlets is an irrelevant metric. The clarity of the code is what matters.

If you want to keep the number of servlets down, while maintaining good separation of functionality, implement the Front Controller pattern. Read this article for more info.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
As Bear said, this seems like a good candidate for the front controller pattern.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Dhaval J. Patel
Ranch Hand

Joined: Mar 10, 2011
Posts: 90

Thanks for your answers. I will try the front controller pattern . I'll post if I run into problems.

Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Web app design confusion