A simple java servlet which gets requests from a web page as parameter, process the request, set response attributes and forwards to destination page
----------------------------------------------
package com.myairline.servlets;
import java.io.*;
import java.net.*;
import java.sql.*;
import java.util.ArrayList;
import java.util.Iterator;
import javax.servlet.*;
import javax.servlet.http.*;
import com.myairline.db.DatabaseConn;
import com.myairline.db.AirportDB;
import com.myairline.db.FlightDB;
import com.myairline.db.PassengerDB;
import com.myairline.db.TicketDB;
import com.myairline.type.Airport;
import com.myairline.type.Flight;
import com.myairline.type.Ticket;
import com.myairline.type.Passenger;
import com.myairline.type.CreditCard;
import com.myairline.common.MyConstants;
public class SearchServlet extends HttpServlet {
AirportDB airportCheck;
ArrayList airportList;
FlightDB flightCheck;
ArrayList flightList1, flightList2;
PassengerDB passengerCheck;
TicketDB ticketCheck;
Flight currentFlight, currentFlight2 ;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String destinationPage = "";
DatabaseConn dbConn = new DatabaseConn();
Connection connection = dbConn.getConnection();
String cmd = (String) request.getParameter("cmd");
HttpSession sessionVar = request.getSession();
if( cmd.equalsIgnoreCase("flight_search") ) {
String source = (String) request.getParameter("source");
airportCheck = new AirportDB();
ArrayList airportList = airportCheck.getAirportList();
if( source.equalsIgnoreCase("customer") )
destinationPage = "/customer/flightSearch.jsp";
else if( source.equalsIgnoreCase("agency") )
destinationPage = "/agency/flightSearch.jsp";
request.setAttribute( "airportList", airportList );
}
else if( cmd.equalsIgnoreCase("get_flights") ) {
String returnFlight = (String) request.getParameter("return");
int fromAirportId = Integer.parseInt( request.getParameter("fromAirportsList") );
int toAirportId = Integer.parseInt( request.getParameter("toAirportsList") ) ;
int day1 = Integer.parseInt( request.getParameter("daydropdown1") );
int month1 = Integer.parseInt( request.getParameter("monthdropdown1") );
int year1 = Integer.parseInt( request.getParameter("yeardropdown1") );
Date departDate = new Date( year1-1900, month1, day1 );
int passengerCount = Integer.parseInt( request.getParameter("passengerCount") );
flightCheck = new FlightDB();
flightList1 = flightCheck.getFlightList( fromAirportId,toAirportId,departDate,passengerCount);
Date returnDate = null;
if( returnFlight.equalsIgnoreCase( "yes" ) ) {
int day2 = Integer.parseInt( request.getParameter("daydropdown2") );
int month2 = Integer.parseInt( request.getParameter("monthdropdown2") );
int year2 = Integer.parseInt( request.getParameter("yeardropdown2") );
returnDate = new Date( year2-1900, month2, day2 );
flightList2 = flightCheck.getFlightList( toAirportId,fromAirportId,returnDate,passengerCount);
request.setAttribute( "flightList2", flightList2 );
}
String source = (String) request.getParameter("source");
if( source.equalsIgnoreCase("customer") )
destinationPage = "/customer/flightResults.jsp";
else if( source.equalsIgnoreCase("agency") )
destinationPage = "/agency/flightResults.jsp";
request.setAttribute( "passengerCount", ""+passengerCount );
request.setAttribute( "flightList1", flightList1 );
}
else if( cmd.equalsIgnoreCase("selected_flight") ) {
Iterator flightIterator ,flightIterator2;
String source = (String) request.getParameter("source");
int flightSelection = Integer.parseInt( request.getParameter("flightSelection") );
int passengerCount = Integer.parseInt( request.getParameter("passengerCount") );
request.setAttribute( "passengerCount", ""+passengerCount );
flightIterator = flightList1.iterator();
while( flightIterator.hasNext() ) {
currentFlight = (Flight) flightIterator.next();
if( currentFlight.getFlightNo() == flightSelection ) {
request.setAttribute( "selectedFlight", currentFlight );
}
}
if( request.getParameter("flightSelection2") != null ) {
int flightSelection2 = Integer.parseInt( request.getParameter("flightSelection2") );
flightIterator2 = flightList2.iterator();
while( flightIterator2.hasNext() ) {
currentFlight2 = (Flight) flightIterator2.next();
if( currentFlight2.getFlightNo() == flightSelection2 ) {
request.setAttribute( "selectedFlight2", currentFlight2 );
}
}
}
if( source.equalsIgnoreCase("customer") )
destinationPage = "/customer/payment.jsp";
else if( source.equalsIgnoreCase("agency") ) {
String saleSelection = (String) request.getParameter("saleSelection");
if( saleSelection.equalsIgnoreCase( "single" ) )
destinationPage = "/agency/singleSale.jsp";
else if( saleSelection.equalsIgnoreCase( "group" ) )
destinationPage = "/agency/groupSale.jsp";
}
} else if( cmd.equalsIgnoreCase("payFlight") ) {
ArrayList departTicketList = new ArrayList();
ArrayList returnTicketList = new ArrayList();
ArrayList passengerList = new ArrayList();
int selectedFlightNo, selectedFlightNo2;
selectedFlightNo = Integer.parseInt( (String) request.getParameter("flightNo") ) ;
CreditCard cardInfo = new CreditCard();
cardInfo.setCardOwner( (String) request.getParameter("cardOwner") );
cardInfo.setCardNo( (String) request.getParameter("cardNo") );
int day = Integer.parseInt( request.getParameter("daydropdown1") );
int month = Integer.parseInt( request.getParameter("monthdropdown1") );
int year = Integer.parseInt( request.getParameter("yeardropdown1") );
cardInfo.setExpireDate( new Date( year-1900, month, day ) );
cardInfo.setSecurityCode( (String) request.getParameter("securityCode") ) ;
int passengerCount = Integer.parseInt( request.getParameter("passengerCount") );
for( int i=1; i<=passengerCount ; i++ ) {
Passenger passenger = new Passenger();
passenger.setName( (String) request.getParameter("name"+i) );
passenger.setSurname( (String) request.getParameter("surname"+i) ) ;
passenger.setAge( Integer.parseInt( (String) request.getParameter("age"+i) ) );
passenger.setSex( request.getParameter("gender"+i).charAt(0) );
passenger.setPassportId( (String) request.getParameter("passportId"+i) ) ;
passenger.setAddress( (String) request.getParameter("address"+i) );
passenger.setPhoneNo( (String) request.getParameter("phoneNo"+i) ) ;
Ticket departTicket = new Ticket();
departTicket.setFlightNo( selectedFlightNo );
departTicket.setFarePaid( currentFlight.getTicketFare() );
departTicket.setTicketStatus( "active" );
departTicket.setMyFlight( currentFlight );
ticketCheck = new TicketDB();
int idTicket = ticketCheck.insertTicket( departTicket );
passenger.setIdTicket( idTicket );
departTicket.setIdTicket( idTicket );
passengerCheck = new PassengerDB();
boolean check = passengerCheck.insertNewPassenger( passenger );
if( request.getParameter("flightNo2") != null ) {
Passenger passenger2 = new Passenger( passenger );
selectedFlightNo2 = Integer.parseInt( (String) request.getParameter("flightNo2") ) ;
Ticket returnTicket = new Ticket();
returnTicket.setFlightNo( selectedFlightNo2 );
returnTicket.setFarePaid( currentFlight2.getTicketFare() );
returnTicket.setTicketStatus( "active" );
returnTicket.setMyFlight( currentFlight2 );
ticketCheck = new TicketDB();
int idTicket2 = ticketCheck.insertTicket( returnTicket );
passenger2.setIdTicket( idTicket2 );
returnTicket.setIdTicket( idTicket2 );
boolean check2 = passengerCheck.insertNewPassenger( passenger2 );
returnTicketList.add( returnTicket );
}
departTicketList.add( departTicket );
passengerList.add( passenger );
}
if( request.getParameter("flightNo2") != null ) {
request.setAttribute( "ticketList2", returnTicketList );
request.setAttribute( "flight2", currentFlight2 );
}
request.setAttribute( "flight", currentFlight );
request.setAttribute( "passengerCount", ""+passengerCount );
request.setAttribute( "ticketList", departTicketList );
request.setAttribute( "passengerList", passengerList );
String source = (String) request.getParameter("source");
if( source.equalsIgnoreCase("customer") )
destinationPage = "/customer/showTicket.jsp";
else if( source.equalsIgnoreCase("agency") )
destinationPage = "/agency/showTicket.jsp";
}
else if( cmd.equalsIgnoreCase("payGroupFlight") ) {
int agencyId = Integer.parseInt( (String) request.getParameter("agencyId") );
int passengerCount = Integer.parseInt( request.getParameter("passengerCount") );
int departFlightNo = Integer.parseInt( (String) request.getParameter("flightNo") ) ;
if( request.getParameter("flightNo2") != null ) {
int returnFlightNo = Integer.parseInt( (String) request.getParameter("flightNo2") ) ;
}
if( request.getParameter("flightNo2") != null ) {
request.setAttribute( "flight2", currentFlight2 );
}
request.setAttribute( "flight", currentFlight );
request.setAttribute( "passengerCount", ""+passengerCount );
destinationPage = "/agency/showGroupTicket.jsp";
}
dbConn.closeConnection();
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(destinationPage);
dispatcher.forward(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
public String getServletInfo() {
return "Short description";
}
}