Now that you have a good understanding of the basic and advanced concepts of Java servlets, turn your attention to the sample MVC application you'll build as a way of learning each new technology supported by WebLogic Server. Quickly revisit the architecture of the Airline Ticket Booking System MVC application that you defined on Day 2. The architecture of the Airline Ticket Booking System consists of three primary layers:
The first technology that you have studied is Java servlets. In the MVC architecture, Java servlets perform the role of the controller. The controller is responsible for routing requests and responses to different components in the model and the view. Ideally, there must be only one controller servlet in an MVC architecture. Hence, for the Airline Ticket Booking System, you will develop and implement one Java servlet, AirlineTicketBookingServlet, given in Appendix C. Now you will study the controller servlet code in detail. Study the sequence diagram in Figure 4.9, which illustrates the working of this servlet. The figure shows an overview of the functions performed by the servlet. Figure 4.9. Sequence diagram for the AirlineTicket BookingServlet of the MVC application.You define the controller servlet class in the package com.sams.learnweblogic7.airlines.servlets using the statement at the start of the code: package com.sams.learnweblogic7.airlines.servlets; After this, you begin importing the packages for the different classes that you will be using in the controller servlet. Three kinds of classes will be imported application-specific packages, standard Java packages, and extensions to Java packages: //Import application related packages import com.sams.learnweblogic7.airlines.businessobject.*; import com.sams.learnweblogic7.airlines.constants.*; import com.sams.learnweblogic7.airlines.entitybean.*; import com.sams.learnweblogic7.airlines.sessionbean.*; import com.sams.learnweblogic7.airlines.exception.*; //importing java extension packages import javax.servlet.*; import javax.servlet.http.*; //import standard java packages io,util and math import java.io.*; import java.util.*; The first method in the life cycle of the controller servlet is the init() method. It defines the initialization of the Array used by the servlet to look up the JSPs that correspond to the action ID: public void init(ServletConfig config) throws ServletException{ super.init(config); Array rulesVariables = ... ... }//end of init The next steps in the servlet life cycle manage requests and responses. Since requests can be either GET or POST HTTP requests, you will handle the processing for these request types using the doGet() and doPost() methods. The doGet() and doPost() methods form the core of the controller servlet's processing. Take a look at the doGet() method: public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException{ doPost(request, response); } The doGet() method in your controller servlet intercepts the GET HTTP request and delegates the request to the doPost() method. The doGet() method passes the HttpServletRequest and HttpServletResponse objects to the doPost() method. This allows the processing logic in the doPost() method to read the request data and return a response. The doPost() method, where a significant amount of processing occurs, is explained in parts: public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ try { //STEP-1 => Get the MVCAppValueObject from the HttpServletRequest MVCAppValueObject inputValueObject = MVCAppValueFactory.getMVCAppValueObject(req); //STEP-2 => Call the business method (if required) and get the // output MVCAppValueObject MVCAppValueObject outputValueObject = businessMethod(inputValueObject); //STEP-3 => Pass the outputValueObject to the respective JSP based on action id callJSP(req, resp, outputValueObject); }//end of try block catch(... ... }//end of catch block finally { ... }//end of finally block }//end of doPost The doPost() method takes the HttpServletRequest and HttpServletResponse objects as parameters. The first step in the doPost() method is to convert the HttpServletRequest object into an MVCAppValueObject using the getMVCAppValueObject() method of the MVCAppValueFactory class. The getMVCAppValueObject() method takes an HttpServletRequest object as a parameter and returns a populated MVCAppValueObject. Note What is the advantage of this approach? Since the HttpServletRequest data needs to be passed along to any component of the model layer, if you pass the HttpServletRequest object (request) directly, then you would be unnecessarily importing the javax.servlet packages in the components of the model layer. This goes against your design of isolating the different layers. Interfacing two layers of your architecture is possible only by using well-defined design contracts. In this case, the design contract between the controller layer (that is, the Java servlet) and the model layer (that is, the EJB components and Java classes) or the model layer and the view layer is the MVCAppValueObject. Data between two layers will always be passed as a populated MVCAppValueObject. The MVCAppValueObject is modeled on the ValueObject design pattern. After obtaining the MVCAppValueObject, you extract the actionID to determine the action to be performed. Depending on the actionID sent in the request, the controller servlet will route the request to the appropriate component of the model layer. This set of functions is performed in the businessMethod() method. This method takes the MVCAppValueObject as input. Depending on the action ID, it then creates the corre sponding EJB, passes the required values to the EJB, and gets the corresponding output MVCAppValueObject. You will be reading about EJBs and their types in greater depth in the days ahead. The controller servlet handles the following action IDs:
In the coming days you will be looking in detail at how to implement the functionality of the components in the model layer. The controller servlet's responsibility, as you saw, is limited to routing requests and responses between the model and view layers and managing the work flow of the application. |