Java Reference
In-Depth Information
L ISTING 13.1
Continued
Service service = (Service)cls.newInstance();
// Execute the Transaction
service.execute(request, response, context);
}
This section of code acts as a Service factory. It parses the service parameter of the request
and creates the class dynamically by creating a new instance of the Service class using its
physical name. It then calls the execute() method which, as you will see in the next section,
is a method defined in the Service interface. This is where the business logic will exist.
The second section of the Controller that we need to examine is the forward() method. It is
used to pass the results of the executed Service to a JSP for viewing. The key logic in the for-
ward method can be found in the following code snippet:
try {
ServletContext context = getServletContext();
if ( !target.equals(“false”) ) {
RequestDispatcher dispatcher =
context.getRequestDispatcher(target);
dispatcher.forward(request, response);
}
13
}
In this section the Controller creates a RequestDispatcher object and calls its forward()
method, passing it the request and response objects and the targeted JSP for viewing.
The Service Interface
Now that we have defined the Controller, let's define the Service interface that will act as the
prototype of all services. For this interface you will have a single method, execute() , that
accepts the necessary parameters to receive and pass HTTP objects to and from the browser.
The listing for the Service can be found in Listing 13.2.
L ISTING 13.2
Service.java
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.ServletContext;
/**
An interface that provides the base functionality for all
Services.
<p>
 
Search WWH ::




Custom Search