J
John English
I'm building a set of servlets and am wondering about the messiness
of my design. I have doGet() and doPost() calling a bunch of other
methods depending on the request, and they need a whole pile of
shared state, some of which is session-wide and some of which is
for that request only (e.g. request-related stuff scooped out of
a database). I ended up passing request and response parameters
all over the place, so I rewrote the state using a RequestWrapper
which adds in all the request-specific state info and a reference
to the response object, so now I can just pass the request wrapper
object to all the methods I call. I also created a SessionState class
containing all the session state info and stored a SessionState object
in the session, so that I don't have to spend all my time wrapping and
casting and unwrapping ints with Integers and all that jazz. The request
wrapper also contains a reference to my SessionState object, as well as
useful stuff like the PrintWriter obtained from the response, to avoid
have to say "PrintWriter out = response.getWriter()" everywhere. Basically
the request wrapper has ended up as this huge dustbin full of all sorts of
miscellaneous junk that needs to be accessible at different points in the
processing (e.g. for the logged-in user, the surname and initials taken
from the database).
Even so, I end up with some really huge servlet classes, with maybe
half-a-dozen methods handling the processing of the different request
parameters (e.g. displaying a table, and responding to different selections
in different ways with the confines of the same web page), and these in
turn divvying up the processing between three of four subsidiary methods.
The lack of modularity that I end up with is driving me crazy. So I'm
wondering:
* should I use request dispatching to split the servlet logic into multiple
servlet classes?
* should I create instances of some other sort of object (ad hoc classes
defined to encapsulate specific aspects of the servlet logic) and use
these to subdivide the work?
* or is there a better way?
I find the whole servlet thing a bit strange and non-OO; there's only
one servlet object, it handles multiple requests, so you can't put any
request-specific or session-specific fields in the servlet class itself
because then they'll be used concurrently by every request and session
that's active at a particular moment, so everything needs to live inside
the methods and be passed around as parameters. Can anyone tell me why
this particular approach was adopted, and how I can ever end up with
readable and maintainable code?
-----------------------------------------------------------------
John English | mailto:[email protected]
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
School of Computing & MIS | ** NON-PROFIT CD FOR CS STUDENTS **
University of Brighton | -- see http://burks.bton.ac.uk
-----------------------------------------------------------------
of my design. I have doGet() and doPost() calling a bunch of other
methods depending on the request, and they need a whole pile of
shared state, some of which is session-wide and some of which is
for that request only (e.g. request-related stuff scooped out of
a database). I ended up passing request and response parameters
all over the place, so I rewrote the state using a RequestWrapper
which adds in all the request-specific state info and a reference
to the response object, so now I can just pass the request wrapper
object to all the methods I call. I also created a SessionState class
containing all the session state info and stored a SessionState object
in the session, so that I don't have to spend all my time wrapping and
casting and unwrapping ints with Integers and all that jazz. The request
wrapper also contains a reference to my SessionState object, as well as
useful stuff like the PrintWriter obtained from the response, to avoid
have to say "PrintWriter out = response.getWriter()" everywhere. Basically
the request wrapper has ended up as this huge dustbin full of all sorts of
miscellaneous junk that needs to be accessible at different points in the
processing (e.g. for the logged-in user, the surname and initials taken
from the database).
Even so, I end up with some really huge servlet classes, with maybe
half-a-dozen methods handling the processing of the different request
parameters (e.g. displaying a table, and responding to different selections
in different ways with the confines of the same web page), and these in
turn divvying up the processing between three of four subsidiary methods.
The lack of modularity that I end up with is driving me crazy. So I'm
wondering:
* should I use request dispatching to split the servlet logic into multiple
servlet classes?
* should I create instances of some other sort of object (ad hoc classes
defined to encapsulate specific aspects of the servlet logic) and use
these to subdivide the work?
* or is there a better way?
I find the whole servlet thing a bit strange and non-OO; there's only
one servlet object, it handles multiple requests, so you can't put any
request-specific or session-specific fields in the servlet class itself
because then they'll be used concurrently by every request and session
that's active at a particular moment, so everything needs to live inside
the methods and be passed around as parameters. Can anyone tell me why
this particular approach was adopted, and how I can ever end up with
readable and maintainable code?
-----------------------------------------------------------------
John English | mailto:[email protected]
Senior Lecturer | http://www.it.bton.ac.uk/staff/je
School of Computing & MIS | ** NON-PROFIT CD FOR CS STUDENTS **
University of Brighton | -- see http://burks.bton.ac.uk
-----------------------------------------------------------------