Sample MVC Application: Controller Servlet

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:

  • Model

  • View

  • Controller

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.

graphics/04fig09.gif

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:

  • LOGIN_ACTION_ID Based on this action ID, the servlet first authenticates the user and then, on successful authentication, gets the passenger information using the PassengerInfoBean EJB. A session is then established for this user, and his or her information is stored in the HttpSession object. Any response to be sent back to the browser will be packaged in the MVCValueObject and forwarded to the view layer.

  • REGISTER_ACTION_ID Based on this action ID, the servlet calls the PassengerInfoBean EJB to create the user profile. Any response to be sent back to the browser will be packaged in the MVCAppValueObject and forwarded to the view layer.

  • VIEW_PROFILE_ACTION_ID Based on this action ID, the servlet calls the getUserProfile() method of the PassengerInfoBean EJB. Any response to be sent back to the browser will be packaged in the MVCAppValueObject and forwarded to the view layer.

  • SEARCH_ACTION_ID Based on this action ID, the servlet calls the AirlineBean EJB and retrieves all flights available for the search criteria defined. Any response to be sent back to the browser will be packaged in the MVCAppValueObject and forwarded to the view layer.

  • SELECTED_FLIGHT_CONFIRM_ACTION_ID Based on this action ID, the servlet calls the AirlineBean EJB. This action occurs when the user confirms his or her flight selection. Any response to be sent back to the browser will be packaged in the MVCAppValueObject and forwarded to the view layer.

  • SELECTED_FLIGHT_CANCEL_ACTION_ID Based on this action ID, the servlet calls the ClientSessionBean EJB and removes the flight selection information from the session. The user is then redirected to the flight search page. Any response to be sent back to the browser will be packaged in the MVCValueObject and forwarded to the view layer.

  • VIEW_USER_BOOKED_FLIGHTS_ACTION_ID Based on this action ID, the servlet calls the bookTicket() method of the BookingRepEJBRemote stateful session EJB. Any response to be sent back to the browser will be packaged in the MVCValueObject and forwarded to the view layer.

  • LOGOUT_USER_ACT_ID Based on this action ID, the servlet destroys the user session and redirects the user to the search page. Any response to be sent back to the browser will be packaged in the MVCAppValueObject and forwarded to the view layer.

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.



Sams Teach Yourself BEA WebLogic Server 7. 0 in 21 Days
Sams Teach Yourself BEA WebLogic Server 7.0 in 21 Days
ISBN: 0672324334
EAN: 2147483647
Year: 2002
Pages: 339

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net