Advanced JSP Sample : Using Model-View-Controller Design

Previous Contents Next

Contents

Purpose

Demonstrate the Model-View-Controller design pattern that can be implemented with JSPs and Servlets on the middle tier.

Remarks

This sample application demonstrates implementing the Model View Controller (MVC) design for an application. This was formerly known as the "Model 2" programming model in JSP specifications.

In the MVC approach, JSPs generate the presentation layer, and either JSPs or Servlets perform process-intensive tasks. The front-end component acts as the controller and is in charge of the request processing and business logic.

In this sample, a controller JSP processes all requests. After completing the processing, it forwards the request to appropriate JSPs for presentation. The Controller is HotelReservation.jsp and the JSPs used for presentation are HotelReservation.jsp, ReservationCart.jsp, and Result.jsp.

The sample is an implementation of a Hotel Reservation Cart where users can store reservation requests in a virtual shopping cart and submit them in batch. All requests for the initial form, adding, deleting and submitting of Reservations are submitted  to the Controller JSP which processes the request and finally forwards it to a JSP for presentation.

The inital JSP has a button to submit the form details which calls the "Controller" JSP, which stores the request in a cart and calls the same JSP again to display the Form along with the added reservation request(s) in the cart. The list will be displayed along with two buttons to Submit or Cancel. These requests will again be submitted to the Controller JSP to take necessary actions. The Submit action involves storing the requests (in cart) into the database, whereas the Cancel button just deletes all the requests from the cart. The logic for performing database operations will be written in the JavaBean. The JSPs in the sample form the "View" component.

 

Code from HotelReservation.jsp

<%@page import="java.sql.*, ReservationBean" %>
<%!
private ReservationBean getRes(HttpServletRequest req) {
// Read inputs from the request object passed in.
String l_arrDate = req.getParameter("P_ARRIVALDATE");
String l_numNights = req.getParameter("P_NUMNIGHTS");
String l_numRooms = req.getParameter("P_NUMROOMS");
String l_name = req.getParameter("P_NAME");
String l_hotel = req.getParameter("P_HOTEL");
StringTokenizer l_tokens = new StringTokenizer(l_hotel,"*");
String l_hotelId = l_tokens.nextToken();
String l_hotelName = l_tokens.nextToken();
String l_roomType = l_tokens.nextToken();
// Instantiate the bean.
ReservationBean resBean = new ReservationBean();
// Initialize the bean properties.
resBean.setHotelId(l_hotelId);
resBean.setArrivalDate(l_arrDate);
resBean.setRoomType(l_roomType);
resBean.setReservedBy(l_name);
resBean.setNumRooms((new Integer(l_numRooms)).intValue());
resBean.setNumNights((new Integer(l_numNights)).intValue());
resBean.setName(l_hotelName);
// Return the object.
return resBean;
}
%>

 

Source Code Listings


Previous Contents Next
E-mail this page
Printer View Printer View
Oracle Is The Information Company About Oracle | Oracle RSS Feeds | Careers | Contact Us | Site Maps | Legal Notices | Terms of Use | Privacy