quick-search.html Front end to travel site <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- Front end to travel servlet. Taken from Core Web Programming Java 2 Edition from Prentice Hall and Sun Microsystems Press, . May be freely used or adapted. --> <HTML> <HEAD> <TITLE>Online Travel Quick Search</TITLE> <LINK REL=STYLESHEET HREF="travel-styles.css" TYPE="text/css"> </HEAD> <BODY> <BR> <H1>Online Travel Quick Search</H1> <FORM ACTION="/servlet/cwp.Travel" METHOD="POST"> <CENTER> Email address: <INPUT TYPE="TEXT" NAME="emailAddress"><BR> Password: <INPUT TYPE="PASSWORD" NAME="password" SIZE=10><BR> Origin: <INPUT TYPE="TEXT" NAME="origin"><BR> Destination: <INPUT TYPE="TEXT" NAME="destination"><BR> Start date (MM/DD/YY): <INPUT TYPE="TEXT" NAME="startDate" SIZE=8><BR> End date (MM/DD/YY): <INPUT TYPE="TEXT" NAME="endDate" SIZE=8><BR> <P> <TABLE CELLSPACING=1> <TR> <TH> <IMG SRC="airplane.gif" WIDTH=100 HEIGHT=29 ALIGN="TOP" ALT="Book Flight"> <TH> <IMG SRC="car.gif" WIDTH=100 HEIGHT=31 ALIGN="MIDDLE" ALT="Rent Car"> <TH> <IMG SRC="bed.gif" WIDTH=100 HEIGHT=85 ALIGN="MIDDLE" ALT="Find Hotel"> <TH> <IMG SRC="passport.gif" WIDTH=71 HEIGHT=100 ALIGN="MIDDLE" ALT="Edit Account"> <TR> <TH><SMALL> <INPUT TYPE="SUBMIT" NAME="flights" VALUE="Book Flight"> </SMALL> <TH><SMALL> <INPUT TYPE="SUBMIT" NAME="cars" VALUE="Rent Car"> </SMALL> <TH><SMALL> <INPUT TYPE="SUBMIT" NAME="hotels" VALUE="Find Hotel"> </SMALL> <TH><SMALL> <INPUT TYPE="SUBMIT" NAME="account" VALUE="Edit Account"> </SMALL> </TABLE> </CENTER> </FORM> <BR> <P ALIGN="CENTER"> <B>Not yet a member? Get a free account <A HREF="accounts.jsp">here</A>.</B></P> </BODY> </HTML> Travel.java Servlet used by travel site. Uses the MVC architecture in that servlet just does computation; all presentation done by JSP. Uses the TravelCustomer bean. package cwp; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; /** Top-level travel-processing servlet. This servlet sets up * the customer data as a bean, then forwards the request * to the airline booking page, the rental car reservation * page, the hotel page, the existing account modification * page, or the new account page. * <P> * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems Press, * . * May be freely used or adapted. */ public class Travel extends HttpServlet { private TravelCustomer[] travelData; public void init() { travelData = TravelData.getTravelData(); } /** Since password is being sent, use POST only. However, * the use of POST means that you cannot forward * the request to a static HTML page, since the forwarded * request uses the same request method as the original * one, and static pages cannot handle POST. Solution: * have the "static" page be a JSP file that contains * HTML only. That's what accounts.jsp is. The other * JSP files really need to be dynamically generated, * since they make use of the customer data. */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String emailAddress = request.getParameter("emailAddress"); String password = request.getParameter("password"); TravelCustomer customer = TravelCustomer.findCustomer(emailAddress, travelData); if ((customer == null) || (password == null) || (!password.equals(customer.getPassword()))) { gotoPage("/travel/accounts.jsp", request, response); } // The methods that use the following parameters will // check for missing or malformed values. customer.setStartDate(request.getParameter("startDate")); customer.setEndDate(request.getParameter("endDate")); customer.setOrigin(request.getParameter("origin")); customer.setDestination(request.getParameter ("destination")); HttpSession session = request.getSession(true); session.setAttribute("customer", customer); if (request.getParameter("flights") != null) { gotoPage("/travel/BookFlights.jsp", request, response); } else if (request.getParameter("cars") != null) { gotoPage("/travel/RentCars.jsp", request, response); } else if (request.getParameter("hotels") != null) { gotoPage("/travel/FindHotels.jsp", request, response); } else if (request.getParameter("cars") != null) { gotoPage("/travel/EditAccounts.jsp", request, response); } else { gotoPage("/travel/IllegalRequest.jsp", request, response); } } private void gotoPage(String address, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address); dispatcher.forward(request, response); } } TravelCustomer Bean package cwp; import java.util.*; import java.text.*; /** Describes a travel services customer. Implemented * as a bean with some methods that return data in HTML * format, suitable for access from JSP. * <P> * Taken from Core Web Programming Java 2 Edition * from Prentice Hall and Sun Microsystems Press, * . * May be freely used or adapted. */ public class TravelCustomer { private String emailAddress, password, firstName, lastName; private String creditCardName, creditCardNumber; private String phoneNumber, homeAddress; private String startDate, endDate; private String origin, destination; private FrequentFlyerInfo[] frequentFlyerData; private RentalCarInfo[] rentalCarData; private HotelInfo[] hotelData; public TravelCustomer(String emailAddress, String password, String firstName, String lastName, String creditCardName, String creditCardNumber, String phoneNumber, String homeAddress, FrequentFlyerInfo[] frequentFlyerData, RentalCarInfo[] rentalCarData, HotelInfo[] hotelData) { setEmailAddress(emailAddress); setPassword(password); setFirstName(firstName); setLastName(lastName); setCreditCardName(creditCardName); setCreditCardNumber(creditCardNumber); setPhoneNumber(phoneNumber); setHomeAddress(homeAddress); setStartDate(startDate); setEndDate(endDate); setFrequentFlyerData(frequentFlyerData); setRentalCarData(rentalCarData); setHotelData(hotelData); } public String getEmailAddress() { return(emailAddress); } public void setEmailAddress(String emailAddress) { this.emailAddress = emailAddress; } public String getPassword() { return(password); } public void setPassword(String password) { this.password = password; } public String getFirstName() { return(firstName); } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return(lastName); } public void setLastName(String lastName) { this.lastName = lastName; } public String getFullName() { return(getFirstName() + " " + getLastName()); } public String getCreditCardName() { return(creditCardName); } public void setCreditCardName(String creditCardName) { this.creditCardName = creditCardName; } public String getCreditCardNumber() { return(creditCardNumber); } public void setCreditCardNumber(String creditCardNumber) { this.creditCardNumber = creditCardNumber; } public String getCreditCard() { String cardName = getCreditCardName(); String cardNum = getCreditCardNumber(); cardNum = cardNum.substring(cardNum.length() - 4); return(cardName + " (XXXX-XXXX-XXXX-" + cardNum + ")"); } public String getPhoneNumber() { return(phoneNumber); } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getHomeAddress() { return(homeAddress); } public void setHomeAddress(String homeAddress) { this.homeAddress = homeAddress; } public String getStartDate() { return(startDate); } public void setStartDate(String startDate) { this.startDate = startDate; } public String getEndDate() { return(endDate); } public void setEndDate(String endDate) { this.endDate = endDate; } public String getOrigin() { return(origin); } public void setOrigin(String origin) { this.origin = origin; } public String getDestination() { return(destination); } public void setDestination(String destination) { this.destination = destination; } public FrequentFlyerInfo[] getFrequentFlyerData() { return(frequentFlyerData); } public void setFrequentFlyerData(FrequentFlyerInfo[] frequentFlyerData) { this.frequentFlyerData = frequentFlyerData; } public String getFrequentFlyerTable() { FrequentFlyerInfo[] frequentFlyerData = getFrequentFlyerData(); if (frequentFlyerData.length == 0) { return("<I>No frequent flyer data recorded.</I>"); } else { String table = "<TABLE>\n" + " <TR><TH>Airline<TH>Frequent Flyer Number\n"; for(int i=0; i<frequentFlyerData.length; i++) { FrequentFlyerInfo info = frequentFlyerData[i]; table = table + "<TR ALIGN=\"CENTER\">" + "<TD>" + info.getAirlineName() + "<TD>" + info.getFrequentFlyerNumber() + "\n"; } table = table + "</TABLE>\n"; return(table); } } public RentalCarInfo[] getRentalCarData() { return(rentalCarData); } public void setRentalCarData(RentalCarInfo[] rentalCarData) { this.rentalCarData = rentalCarData; } public HotelInfo[] getHotelData() { return(hotelData); } public void setHotelData(HotelInfo[] hotelData) { this.hotelData = hotelData; } // This would be replaced by a database lookup // in a real application. public String getFlights() { String flightOrigin = replaceIfMissing(getOrigin(), "Nowhere"); String flightDestination = replaceIfMissing(getDestination(), "Nowhere"); Date today = new Date(); DateFormat formatter = DateFormat.getDateInstance(DateFormat.MEDIUM); String dateString = formatter.format(today); String flightStartDate = replaceIfMissing(getStartDate(), dateString); String flightEndDate = replaceIfMissing(getEndDate(), dateString); String [][] flights = { { "Java Airways", "1522", "455.95", "Java, Indonesia", "Sun Microsystems", "9:00", "3:15" }, { "Servlet Express", "2622", "505.95", "New Atlanta", "New Atlanta", "9:30", "4:15" }, { "Geek Airlines", "3.14159", "675.00", "JHU", "MIT", "10:02:37", "2:22:19" } }; String flightString = ""; for(int i=0; i<flights.length; i++) { String[] flightInfo = flights[i]; flightString = flightString + getFlightDescription(flightInfo[0], flightInfo[1], flightInfo[2], flightInfo[3], flightInfo[4], flightInfo[5], flightInfo[6], flightOrigin, flightDestination, flightStartDate, flightEndDate); } return(flightString); } private String getFlightDescription(String airline, String flightNum, String price, String stop1, String stop2, String time1, String time2, String flightOrigin, String flightDestination, String flightStartDate, String flightEndDate) { String flight = "<P><BR>\n" + "<TABLE WIDTH=\"100%\"><TR><TH CLASS=\"COLORED\">\n" + "<B>" + airline + " Flight " + flightNum + " ($" + price + ")</B></TABLE><BR>\n" + "<B>Outgoing:</B> Leaves " + flightOrigin + " at " + time1 + " AM on " + flightStartDate + ", arriving in " + flightDestination + " at " + time2 + " PM (1 stop -- " + stop1 + ").\n" + "<BR>\n" + "<B>Return:</B> Leaves " + flightDestination + " at " + time1 + " AM on " + flightEndDate + ", arriving in " + flightOrigin + " at " + time2 + " PM (1 stop -- " + stop2 + ").\n"; return(flight); } private String replaceIfMissing(String value, String defaultValue) { if ((value != null) && (value.length() > 0)) { return(value); } else { return(defaultValue); } } public static TravelCustomer findCustomer (String emailAddress, TravelCustomer[] customers) { if (emailAddress == null) { return(null); } for(int i=0; i<customers.length; i++) { String custEmail = customers[i].getEmailAddress(); if (emailAddress.equalsIgnoreCase(custEmail)) { return(customers[i]); } } return(null); } } BookFlights.jsp, RentCars.jsp, FindHotels.jsp, EditAccounts.jsp, and IllegalRequest.jsp Pages used by the Travel servlet to do its presentation. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- Flight-finding page for travel example. Taken from Core Web Programming Java 2 Edition from Prentice Hall and Sun Microsystems Press, . May be freely used or adapted. --> <HTML> <HEAD> <TITLE>Best Available Flights</TITLE> <LINK REL=STYLESHEET HREF="/travel/travel-styles.css" TYPE="text/css"> </HEAD> <BODY> <H1>Best Available Flights</H1> <CENTER> <jsp:useBean id="customer" class="cwp.TravelCustomer" scope="session" /> Finding flights for <jsp:getProperty name="customer" property="fullName" /> <P> <jsp:getProperty name="customer" property="flights" /> <P><BR><HR><BR> <FORM ACTION="/servlet/BookFlight"> <jsp:getProperty name="customer" property="frequentFlyerTable" /> <P> <B>Credit Card:</B> <jsp:getProperty name="customer" property="creditCard" /> <P> <INPUT TYPE="SUBMIT" NAME="holdButton" VALUE="Hold for 24 Hrs"> <P> <INPUT TYPE="SUBMIT" NAME="bookItButton" VALUE="Book It!"> </FORM> </CENTER> </BODY> </HTML> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- Car rental page not implemented -- see airline booking page. Taken from Core Web Programming Java 2 Edition from Prentice Hall and Sun Microsystems Press, . May be freely used or adapted. --> <HTML> <HEAD> <TITLE>Not Implemented</TITLE> <LINK REL=STYLESHEET HREF="/travel/travel-styles.css" TYPE="text/css"> </HEAD> <BODY> <H1>Car Rental Page Not Implemented</H1> <CENTER> Hey, this is only an example. See the airline booking page. </CENTER> </BODY> </HTML> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- Hotel page not implemented -- see airline booking page. Taken from Core Web Programming Java 2 Edition from Prentice Hall and Sun Microsystems Press, . May be freely used or adapted. --> <HTML> <HEAD> <TITLE>Not Implemented</TITLE> <LINK REL=STYLESHEET HREF="/travel/travel-styles.css" TYPE="text/css"> </HEAD> <BODY> <H1>Hotel Page Not Implemented</H1> <CENTER> Hey, this is only an example. See the airline booking page. </CENTER> </BODY> </HTML> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- Account page not implemented -- see airline booking page. Taken from Core Web Programming Java 2 Edition from Prentice Hall and Sun Microsystems Press, . May be freely used or adapted. --> <HTML> <HEAD> <TITLE>Not Implemented</TITLE> <LINK REL=STYLESHEET HREF="/travel/travel-styles.css" TYPE="text/css"> </HEAD> <BODY> <H1>Accounts Page Not Implemented</H1> <CENTER> Hey, this is only an example. See the airline booking page. </CENTER> </BODY> </HTML> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <!-- Illegal request at Travel Quick Search page. Taken from Core Web Programming Java 2 Edition from Prentice Hall and Sun Microsystems Press, . May be freely used or adapted. --> <HTML> <HEAD> <TITLE>Illegal Request</TITLE> <LINK REL=STYLESHEET HREF="/travel/travel-styles.css" TYPE="text/css"> </HEAD> <BODY> <H1>Illegal Request</H1> <CENTER>Please try again.</CENTER> </BODY> </HTML>
Aug 29