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
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.
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.