Howto - Command pattern with servlet

A

asaguden

Hi,
The system I have developed is currently suffering from an
overweight controller (servlet).
After reading some good books on J2EE design patterns, I see that the
Command pattern should suit me just fine to cleanup and separate
responsibility. So far so good.

But after reading some more I learned that it is good practice
to decouple the HttpServletXXX components. That is, pick out the
interesting stuff in the web container (servlet, jsp) and then
continue processing.

But Command pattern just redirect HttpServletRequest to the Command
class.

Q: Shall I decouple the HttpServletRequest before calling the command
OR is the Command part of the web container...?

/ Peter
 
I

iksrazal

asaguden said:
Hi,
The system I have developed is currently suffering from an
overweight controller (servlet).
After reading some good books on J2EE design patterns, I see that the
Command pattern should suit me just fine to cleanup and separate
responsibility. So far so good.

But after reading some more I learned that it is good practice
to decouple the HttpServletXXX components. That is, pick out the
interesting stuff in the web container (servlet, jsp) and then
continue processing.

But Command pattern just redirect HttpServletRequest to the Command
class.

Q: Shall I decouple the HttpServletRequest before calling the command
OR is the Command part of the web container...?

/ Peter

The clearest way I found to do this is to use a RequestHelper object
to process the servlet request, determine the Command object to
process, and then execute that Command in the servlet. Sorta like
(doGet() and doPost() both call processRequest()):

public void processRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String resultPage = null;
try
{
RequestHelper rh = new SelicRequestHelper (request,response);
if (rh.isAuthorized())
{
Command command = rh.getCommand();
resultPage = command.execute(rh);
ServletContext context = getServletContext();
RequestDispatcher dispatcher =
context.getRequestDispatcher(resultPage);
dispatcher.forward(request,response);
}
}
catch (Exception exception)
{
Fwlog.error(this, Fwlog.DB, "front controller stack trace
caught");
Fwlog.error(this, Fwlog.DB, exception);
request.setAttribute(SelicKeys.EBBeanName, exception);
ServletContext context = getServletContext();
RequestDispatcher dispatcher =
context.getRequestDispatcher(SelicKeys.ERROR_PAGE);
dispatcher.forward(request,response);
}
}

The best book I have found covering the Command pattern with servlets
is 'J2EE Design Patterns Applied' . The source code is very helpful
there - probably worth tracking down for some good examples.

HTH,
iksrazal
http://www.braziloutsource.com/
 
A

asaguden

Good,
that was sort of the way I was figuring it.

Follow-up Q:
- Then I assume the Command is responsible for querying databases,
files etc ?
- Does the Command return control to the servlet after it has finished
its work
for forwarding work to be done by the Servlet ?
/ Peter
 
R

RS

Really you should create a "business tier". The command should do your
"web" related logic. Send it to the "business tier' which would then
go the Database and parse out the data. This would creat this n-tier
structure, which would very cleearl separate out the different levels
of the Web application. What to do point is hard to determine since I
don't know your app.

The command should send page the "page" to forward o back to the
Servlet and the forwarding work dhould be done by the servlet. The
servlet should not really do any logic.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top