BookOrdering
Order
User
Book
JSPHelper
package com.bookshop.logic;
/**
* Bookshop
*
* JSP and Servlet Combination for Webprogramming
*
* @author Horst Leitenmüller, 9255833 / 881
* @version 1.0
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.sql.SQLException;
import javax.servlet.*;
import javax.servlet.http.*;
import com.bookshop.database.*;
public class BookOrdering extends HttpServlet
{
////////////////////////////////////////////////////////////////////////////
// Class Variables
////////////////////////////////////////////////////////////////////////////
// JSP Helper Class for encode URL
JSPHelper jsph = new JSPHelper();
// Accessor for the Database
Accessor accessor;
// Servlet Name for Url Encoding
String servletName= "";
// Order List for the Books
Vector orderList;
// variables for checking, which Browser requests
final static int HTML = 1, WML = 2, VXML=3;
// pathvariables for Sourcedestinationof HTML and WML and VXML
String PATHWML = "";
String PATHHTML = "";
String PATHVXML = "";
///////////////////////////////////////////////////////////////////
// initialisation of the Servlet
///////////////////////////////////////////////////////////////////
public void init(ServletConfig config) throws ServletException
{
super.init(config);
String value;
// check the Databasetype and init
this.accessor = new Accessor();
if ((value = config.getInitParameter("Servlet_Name")) == null)
{
throw new ServletException("Servlet Name not available");
}
else
{
servletName = value;
}
jsph.setServletName(servletName);
if ((value = config.getInitParameter("HTML_Path")) == null)
{
PATHHTML = "";
}
else
{
PATHHTML = value;
}
if ((value = config.getInitParameter("WML_Path")) == null)
{
PATHWML = "";
}
else
{
PATHWML = value;
}
if ((value = config.getInitParameter("VXML_Path")) == null)
{
PATHVXML = "";
}
else
{
PATHVXML = value;
}
if ((value = config.getInitParameter("dbDriver")) == null)
{
throw new ServletException("FATAL ERROR: Parameter dbDriver not found");
}
else
{
accessor.setDriver(value);
}
if ((value = config.getInitParameter("dbConnect")) == null)
{
throw new ServletException("DB-Connection failed");
}
else
{
accessor.setURL(value);
}
// db user and password (optional)
if ((value=config.getInitParameter("dbUser")) != null)
{
accessor.setUser(value);
}
if ((value=config.getInitParameter("dbPassword")) != null)
{
accessor.setPwd(value);
}
// open database
try
{
accessor.getConnection();
} catch (SQLException e)
{
e.printStackTrace();
throw new ServletException("Couldn't open database");
}
}
///////////////////////////////////////////////////////////////////////////
// public Methods for request and response
///////////////////////////////////////////////////////////////////////////
/**
* Process GET requests
*
* @param request
* @param response
*
* @throws IOException
* @throws ServletException
*
* @see
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGetPost(request, response);
}
/**
* Process POST requests
*
* @param request
* @param response
*
* @throws IOException
* @throws ServletException
*
* @see
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
doGetPost(request, response);
}
/**
* Process GET and POST requests, called from doGet and doPost.
*
* @param request
* @param response
*
* @throws IOException
* @throws ServletException
*
* @see
*/
public void doGetPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException,
IOException
{
HttpSession session = request.getSession(true);
if(session.isNew())
{
clearSession(session);
orderList = new Vector();
session.putValue("OrderList", orderList);
}
else
{ // an old session came in
if(((Vector)session.getValue("OrderList"))== null)
{ // session was cleared before
orderList = new Vector();
session.putValue("OrderList", orderList);
}
}
// read the all books of the database
Vector v = accessor.getBooks();
request.setAttribute("booklist", v);
switch(checkBrowserType(request))
{
case HTML:
response.setHeader("Cache-control", "no-cache");
response.setHeader("Pragma", "no-cache");
if (request.getParameter("next") != null)
{
if (request.getParameter("next").equals("update") || request.getParameter("next").equals("delete"))
{
getItemfromRequest(request,response,session);
gotoPage(PATHHTML + "OrderStatus.jsp", request, response);
}
if (request.getParameter("next").equals("order"))
{
gotoPage(PATHHTML + "Registration.jsp", request, response);
} else if (request.getParameter("next").equals("ordandreg"))
{
if(checkRegistration(request))
{
User u = new User(0,
request.getParameter("fname"),
request.getParameter("lname"),
request.getParameter("address"),
request.getParameter("zipcode"));
System.out.println(request.getParameter("fname"));
u = accessor.searchUser(u);
System.out.println("V"+u.getFName());
session.putValue("user", u);
int orderid = accessor.getNewOrderID();
accessor.saveOrder(u, (Vector)session.getValue("OrderList"), new Integer(orderid));
session.putValue("OrderID", new Integer(orderid));
gotoPage(PATHHTML + "Finish.jsp", request, response);
clearSession(session);
}
else
gotoPage(PATHHTML + "Registration.jsp", request, response);
}
}
else
{ // next == null first contact to this Site
gotoPage(PATHHTML + "Booklist.jsp", request, response);
}
break;
case WML: // its only a part implemented
//response.setContentType("text/vnd.wap.wml");
//response.setHeader("Cache-control", "no-cache");
if (request.getParameter("next") != null)
{
if (request.getParameter("next").equals("Order"))
{
gotoPage(PATHWML + "Order.jsp", request, response);
} else if (request.getParameter("next").equals("Registration"))
{
gotoPage(PATHWML + "Registration.jsp", request, response);
} else if (request.getParameter("next").equals("update"))
{
gotoPage(PATHWML + "Finish.jsp", request, response);
}
}
else
{ // next == null first contact to this Site
gotoPage(PATHWML + "Booklist.jsp", request, response);
}
break;
case VXML: // this part is not implemented
//response.setContentType("text/x-vxml");
//response.setHeader("Cache-control", "no-cache");
if (request.getParameter("next") != null)
{
if (request.getParameter("next").equals("Order"))
{
gotoPage(PATHVXML + "Order.jsp", request, response);
} else if (request.getParameter("next").equals("Registration"))
{
gotoPage(PATHVXML + "Registration.jsp", request, response);
} else if (request.getParameter("next").equals("update"))
{
gotoPage(PATHVXML + "Finish.jsp", request, response);
}
}
else
{ // next == null first contact to this Site
gotoPage(PATHVXML + "Booklist.jsp", request, response);
}
break;
default: break;
}
}
////////////////////////////////////////////////////////////////////////////
// Browser Detection
////////////////////////////////////////////////////////////////////////////
/**
* Returns if the requesting browser is a WML or HTML browser.
* NOTE: This is a very simple
* @return WML browser, HTML browser as int or VXML as int
*/
public int checkBrowserType(HttpServletRequest request)
{
String userAgent = request.getHeader("User-Agent");
if ((userAgent == null) ||
userAgent.regionMatches(true, 0, "nokia", 0, 5))
{
return WML;
}
else if (userAgent.regionMatches(true, 0, "Mozilla" , 0, 7) ||
userAgent.regionMatches(true, 0, "MSIE", 0, 4))
{
return HTML;
}
else
return VXML;
}
///////////////////////////////////////////////////////////////////////////////////
// forwards the request to a JSP side
///////////////////////////////////////////////////////////////////////////////////
/**
* fowards the request to the correct Page
*/
private void gotoPage(String address, HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(address);
dispatcher.forward(request, response);
}
///////////////////////////////////////////////////////////////////////////////////
// clearing the Session
///////////////////////////////////////////////////////////////////////////////////
// clears the session and removes the OrderList value
private void clearSession(HttpSession session)
{
session.removeValue("OrderList");
session.removeValue("OrderID");
session.removeValue("user");
}
///////////////////////////////////////////////////////////////////////////////////
// Item Handling
///////////////////////////////////////////////////////////////////////////////////
public void getItemfromRequest(HttpServletRequest request, HttpServletResponse response, HttpSession session)
{
if (request.getParameter("update") != null)
{
int isbn = 0; // the isbn from request
int countage = 0; // the countage of the book choosen
try
{
isbn = Integer.parseInt(request.getParameter("isbn"));
countage = Integer.parseInt(request.getParameter("countage"));
// add the choosen book to the shopping cart
addItem(session, isbn, countage);
}catch (NumberFormatException e){}
}
else if (request.getParameter("delete") != null)
{
int isbn = 0;
try
{
isbn = Integer.parseInt(request.getParameter("isbn"));
deleteItem(session, isbn);
}catch (NumberFormatException e){}
}
}
// adds an Item to the shopping cart
public void addItem(HttpSession session, int isbn, int countage)
{
Vector oL;
oL = (Vector) session.getValue("OrderList");
Order o = null;
Book b = accessor.getBookByISBN(isbn);
if ((o = isalreadyChoosen(isbn,oL)) != null)
{
o.setCountage(o.getCountage()+ countage);
}
else
{
// a new Order for this Item is created
o = new Order(-1, //the user ID is fixed when OrderList is orderd !!
isbn,
countage,
b);
if(oL == null)
oL = new Vector();
// add the Element to the shopping cart
oL.addElement(o);
}
session.putValue("OrderList", oL);
}
// deletes an item of the shopping cart
private void deleteItem(HttpSession session, int isbn)
{
Vector oL = (Vector) session.getValue("OrderList");
Enumeration e = oL.elements();
Order o = (Order)e.nextElement();
while(e.hasMoreElements() && o.getISBN() != isbn)
o = (Order) e.nextElement();
if (o.getISBN() == isbn)
oL.removeElement(o);
}
// check if a book is already choosen and return the order for the book
private Order isalreadyChoosen(int isbn, Vector oL)
{
Order o = null;
if(oL != null && oL.size() >= 1)
{
Enumeration e;
e = oL.elements();
o = (Order) e.nextElement();
// check if the book is already in the order
while (e.hasMoreElements() && o.getISBN() != isbn)
{
o = (Order) e.nextElement();
}
if(o.getISBN() != isbn)
return null;
}
return o;
}
// checks the registration if all fields filled true is returned (not to be filled UserID)
private boolean checkRegistration(HttpServletRequest request)
{
if(request.getParameter("fname") != null)
return !(request.getParameter("fname").equals("")||request.getParameter("lname").equals("")||
request.getParameter("address").equals("")||request.getParameter("zipcode").equals(""));
return false;
}
// closes all Connection and destroys the Servlet
public void destroy()
{
// close database */
try
{
accessor.closeConnection();
} catch (Exception e) {
e.printStackTrace();
}
}
// returns the Servlet Info
public String getServletInfo()
{
return "A Servlet for Book ordering";
}
}
Zurück zum Anfang
package com.bookshop.logic;
public class User
{
////////////////////////////////////////////////////
// class variables
////////////////////////////////////////////////////
int userID;
String fname;
String lname;
String address;
String zipcode;
////////////////////////////////////////////////////
// constructor
////////////////////////////////////////////////////
public User(int userID, String fname, String lname, String address, String zipcode)
{
setUserID(userID);
setFName(fname);
setLName(lname);
setAddress(address);
setZipCode(zipcode);
}
////////////////////////////////////////////////////
// set methods of User
////////////////////////////////////////////////////
public void setUserID(int userID)
{
this.userID = userID;
}
public void setFName(String fname)
{
this.fname = fname;
}
public void setLName(String lname)
{
this.lname = lname;
}
public void setAddress(String address)
{
this.address = address;
}
public void setZipCode(String zipcode)
{
this.zipcode = zipcode;
}
////////////////////////////////////////////////////
// get methods of User
////////////////////////////////////////////////////
public int getUserID()
{
return userID;
}
public String getFName()
{
return fname;
}
public String getLName()
{
return lname;
}
public String getAddress()
{
return address;
}
public String getZipCode()
{
return zipcode;
}
}
Zurück zum Anfang
package com.bookshop.logic;
public class Book
{
////////////////////////////////////////////////////
// class variables
////////////////////////////////////////////////////
int isbn;
String autor;
String title;
////////////////////////////////////////////////////
// constructor
////////////////////////////////////////////////////
public Book(int isbn, String autor, String title)
{
setISBN(isbn);
setAutor(autor);
setTitle(title);
}
////////////////////////////////////////////////////
// set methods of Book
////////////////////////////////////////////////////
public void setISBN(int isbn)
{
this.isbn = isbn;
}
public void setAutor(String autor)
{
this.autor = autor;
}
public void setTitle(String title)
{
this.title = title;
}
////////////////////////////////////////////////////
// get methods of Book
////////////////////////////////////////////////////
public int getISBN()
{
return isbn;
}
public String getAutor()
{
return autor;
}
public String getTitle()
{
return title;
}
}
Zurück zum Anfang
package com.bookshop.logic;
public class Order
{
////////////////////////////////////////////////////
// class variables
////////////////////////////////////////////////////
int orderID;
int userID;
int isbn;
Book book;
int countage;
////////////////////////////////////////////////////
// Constructor
////////////////////////////////////////////////////
// constructor for an Order
public Order(int orderID, int userID, int isbn, int countage, Book book)
{
setOrderID(orderID);
setUserID(userID);
setISBN(isbn);
setCountage(countage);
setBook(book);
}
// constructor for an Order
public Order(int userID, int isbn, int countage, Book book)
{
setUserID(userID);
setISBN(isbn);
setCountage(countage);
setBook(book);
}
////////////////////////////////////////////////////
// set methods of Order
////////////////////////////////////////////////////
public void setOrderID(int orderID)
{
this.orderID = orderID;
}
public void setUserID(int userID)
{
this.userID = userID;
}
public void setISBN(int isbn)
{
this.isbn = isbn;
}
public void setBook(Book b)
{
this.book = b;
}
public void setCountage(int countage)
{
this.countage = countage;
}
////////////////////////////////////////////////////
// get methods of Order
////////////////////////////////////////////////////
public int getOrderID()
{
return orderID;
}
public int getUserID()
{
return userID;
}
public int getISBN()
{
return isbn;
}
public Book getBook()
{
return book;
}
public int getCountage()
{
return countage;
}
}
Zurück zum Anfang
package com.bookshop.logic;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.Properties;
import java.util.Random;
public class JSPHelper
{
private static String servletName;
public static void setServletName(String str)
{
servletName = str;
}
public static String getServletInfo()
{
return servletName;
}
public static String getBaseUrl(HttpServletRequest request, HttpServletResponse response)
{
String encodedURL;
HttpSession session = request.getSession(true);
encodedURL = response.encodeURL(servletName);
return encodedURL;
}
///////////////////////////////////////////////////////////////////////////
// URL Rewriting to avoid Caching by Wap Handy's
///////////////////////////////////////////////////////////////////////////
public static String URLRewriter()
{
return "r=" + getRandom();
}
protected static int getRandom()
{
Random randomizer;
int rnd;
randomizer = new Random();
rnd = Math.abs(randomizer.nextInt());
return rnd;
}
}