RequestProcessor.java.
- By Prashant Sarode
- June 24, 2001
Enterprise Java
Writing a Reusable Implementation of the MVC Design Pattern
by Prashant Sarode
Listing 1. RequestProcessor.java.
package com.framework;
import java.lang.reflect.*;
import java.io.Serializable;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.framework.*;
public class RequestProcessor implements Serializable {
public RequestProcessor(){
RequestToEventTranslator eventTranslator
=new RequestToEventTranslator();
}
public void processRequest(HttpServletRequest request,
HttpServletResponse response,
ServletConfig servletConfig)
throws IOException,
ServletException{
//Step 1-Create a Business Event (abstract intent) from HTTP request.
Event businessEvent =eventTranslator.processRequest
(request,servletConfig);
//Step 2-Get the "functionality" Class name that is supposed to handle this event.
String functionalityClassName = businessEvent.getFunctionalityClass();
//Step 3-Get Reference to functionality from the servlet Context (i.e. application scope). If null then create
Instance and store the reference in servlet Context with class Name as the Key.
if(servletConfig.getServletContext().getAttribute(functionality
ClassName) == null){
//Create Instance Of the Functionality bean.
functionality = new Functionality(functionalityClassName);
servletConfig.getServletContext().setAttribute(functionality
ClassName,(Object) functionality);
}else{
functionality =(Functionality)
servletConfig.getServletContext().getAttribute(functionality
ClassName);
}
//Step 4-Call handleEvent(Event e) on appropriate functionality.
Result resultOfHandleEvent=functionality.handleEvent
(businessEvent);
//Step 5-Forward to appropriate View based on Event Success Or Failure.
if ( resultOfHandleEvent.wasSuccess() ){
//Get the Response View from event.
String responseView = businessEvent.getResponseView();
//Set the Result Object in Request Scope.
request.setAttribute("Result",resultOfHandleEvent);
//Finally go to the Response view.
goToView(responseView, servletConfig , request , response);
}else{
//Get the Error View from event.
String errorView = businessEvent.getErrorView();
//Set the Result Object in Request Scope.
request.setAttribute("Result",resultOfHandleEvent);
//Finally go to Error view.
goToView(errorView, servletConfig , request , response);
}
}
}