• Post Reply Bookmark Topic Watch Topic
  • New Topic

doGet() Bottleneck: Do I need mutliple servlets

 
Gabriel Cane
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm writing a servlet with the idea of creating an intuitive user interface. The idea is that, while entering information, the page layout stays the same and all the functional buttons stay laid out in the same way. My code mostly works, except for a few significant bugs, but my servlet code is a little messy. All my other code is carefully organized into packages, classes and methods according to functionality. I'm in the process of optimizing and refactoring my code, but I've run across a little problem.
I have a huge amount of code in my doGet() method. While this code mostly works, it's not very readable. I would like to break it up into different methods, but there are so many doGet()-specific variables involved (HttpSession, request, response, and variables for my own classes) that it's tricky to break up the code. Add to this the fact that it's impossible to call a specific method of a servlet, only the servlet itself.
In order to solve this code optimization problem, should I:
A) Create a seperate servlet for each level of functionality, possibly creating my own servlet superclass for some common functionality to be used in all the subclasses.
B) Use the same servlet, but attempt to break it down into separate methods, even if my parameter lists will be somewhat long.
Which is the best approach for this problem?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13078
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Based on extensive experience with exactly the same problem, I would go with:
B) Use the same servlet, but attempt to break it down into separate methods, even if my parameter lists will be somewhat long.
Use doGet to decide what to do - pass off the actual page creation to other methods or helper classes. Do NOT worry about the number of parameters you have to pass - the effect on execution time is unmeasurably small.
Bill
 
Kyle Brown
author
Ranch Hand
Posts: 3892
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To reduce the number of variables passed on parameter lists, consider grouping together some of your variables into objects. This is a common technique for discovering objects that you missed...
In fact, it's so common it has a name. It's called "Introduce Parameter Object", and it's one of the dozens of WONDERFUL refactorings in Martin Fowler's excellent book Refactoring: Improving the Design of Existing Code. If you do NOT have this book, then RUN outside to the nearest bookstore and buy a copy. It's an indispensible resource for the kind of work you're doing. But don't just take my advice -- read the reviews of the book...
Kyle
[ January 15, 2002: Message edited by: Kyle Brown ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!