Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Design Java servlets with the Delegation Event Model

Use the idea of Delegation Event Model to build robust and flexible servlet programs

  • Print
  • Feedback
As a servlet developer, you probably know the importance of having a single entry point for all the POST/GET requests in your servlet Web application. With such a front servlet (also referred to as a "request dispatching servlet"), your application has a chance to do the things common to all request handlers, like session verification and so on. You also know that for a complex application you should not put your entire code into one servlet. But what should you do?

We faced such a situation during a programming project several months ago. At the beginning, we used one front servlet and a list of if-else statements to dispatch requests to individual handlers. It worked fine, but as our application continued to evolve, the if-else sequence in our front servlet code became longer and longer -- a definite design concern.

The problem we encountered with the long if-else list led us to the idea of the Delegation Event Model in JDK1.1, which offers a good method for handling GUI events in Java. With this idea in mind, we extended the model to our servlet program. Along with other relevant design improvements, we eventually built a servlet application with a simple, flexible, and extensible architecture. In this article, we will demonstrate our design, and the ideas behind it, for building servlet applications using the delegation event model.

The RequestHandler interface

Our servlet design starts with the RequestHandler interface. Like those XXXListener interfaces in the java.awt.event package, any objects with this interface can be registered to handle a specific event or request. For example, when you add an ActionListerner to a button, the listener's actionPerformed() method will be called when the button is clicked. Similarly, if you add a RequestHandler to a servlet's HTTP POST event, the handler's processRequest() method will be called when the servlet receives this specific POST request.

Here is the RequestHandler interface:

package article.servlet;

import java.io.*; import java.net.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; import article.obj.User;
public interface RequestHandler { public void processRequest(Object user, HttpServletRequest req, HttpServletResponse res, Object[] services, Object log) throws ServletException, IOException; }


Let's examine the interface code. First, all request handlers implement the RequestHandler interface. In its only defined method, processRequest(), the first argument is a User object, which indicates who is making the request. The handler can use this object in the Web request processing. Most GET/POST handlers need to know who the requester is in order to react accordingly. The third argument is a collection of service objects, which can be database connections, RMI server references, or even ORB references needed by the RequestHandler. In today's heterogeneous world, it is common for a Web application to use several databases or remote services. The log PrintStream makes it possible to log information for this application to a separate place.

  • Print
  • Feedback
What is Tech Briefcase?
TechBriefcase is a new, free service where IT Professionals can Search, Store and Share IT white papers and content like this. Learn more
Bookmark content
Speed up your research efforts with content across the web.
Search and Store
Find the white papers you need. Create folders for any topic.
View Anywhere
Open your briefcase on your iPhone, tablet or desktop. Share with colleagues.
Don't have an account yet?

Resources