Java Reference
In-Depth Information
What Is a Controller?
As we described in Chapter 1, “Web Applications and the Model View Controller (MVC)
Design Pattern,” a Controller defines the way the user interface reacts to the user's input. For
our purposes the Controller is the heart of our Web applications. The Controller is what deter-
mines how your incoming requests are handled.
A Servlet Controller
You will be implementing your Controller as a servlet. It will act as a factory to instantiate a
class that is determined by the request. We call these instantiated classes Services . They will be
further defined in the next section.
To create your servlet Controller, you need to create a servlet that takes the incoming request,
parses the named Service, executes the created Service, and then forwards the results to a tar-
get that is also included on the request. An example of a request containing these elements is
listed below:
http://localhost/djs/servlet/Controller?service=Search&target=/
searchresults.jsp
This requests that the Service Search be executed and the results be forwarded to the
searchresults.jsp page. Now let's define a servlet that will represent this Controller. Listing
13.1 contains the source for your Controller servlet.
L ISTING 13.1
Controller.java
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Controller extends HttpServlet {
/**
*
*/
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
/** The forward method forwards the results of the Service to the passed in
target.
@throws VSException if an IOException or ServletException is thrown by the
RequestDispatcher.
Search WWH ::




Custom Search