/* Java Servlet example - Netprog Pizza Server Uses hidden fields. */ import java.io.*; import java.text.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class Pizza extends HttpServlet { HttpServletRequest request; HttpServletResponse response; PrintWriter out; static String path="c:\\jdk1.3\\tomcat\\webapps\\Root\\Pizza\\"; // we only either GET or POST requests public void doPost(HttpServletRequest rq, HttpServletResponse resp) throws IOException, ServletException { doGet(rq,resp); } public void doGet(HttpServletRequest rq, HttpServletResponse resp) throws IOException, ServletException { request = rq; response = resp; // tell the browser we are sending back HTML response.setContentType("text/html"); // grab a writer back to the browser out = response.getWriter(); // now go handle this request HandleRequest(); } // Inside HandleRequest we grab exceptions and deal with them // If we just thow them upward - we end up sending the Java // call stack to the browser (probably not a good idea!) void HandleRequest() throws IOException { try { // send back our logo (always) SendFile(path+"logo.html"); // First - find out what kind of a request this is. // Each form has a hidden field named formname that // tells us which form the request comes from. // Possible values: // nothing - send back login screen // "login" - this is a login attempt // "order" - this is a pizza order String form = request.getParameter("formname"); // If there is no field named "formname" we should // send back a login screen if (form==null) { SendFile(path+"loginform.html"); } else if (form.equals("login")) { // this is a login attempt Login(); } else if (form.equals("order")) { // this is a pizza order Order(); } else { // who knows? SendFile(path+"badrequest"); } } catch (Exception e) { // Any exception is handled by punting... SendFile(path+"error"); } } // Order() handles orders for pizza. // void Order() throws IOException { // Order request - we expect "name" and "password" // Check them before worrying about the pizza and size String name = request.getParameter("name"); String pass = request.getParameter("password"); if (ValidateUser(name,pass)) { // Valid name and password - look at the order // we expect to get "pizza" and "size" fields String pizza = request.getParameter("pizza"); String size = request.getParameter("size"); // First make sure we got a complete order if (ValidateOrder(pizza,size)) { // Everything looks good - print a receipt Receipt(name,pizza,size); } else { // Bad order SendFile(path+"nicetry.html"); } } else { // Invalid login - flame the loser SendFile(path+"baduser.html"); SendFile(path+"loginform.html"); } } // Login() takes care of login requests. If everything is OK // (valid name and password) we send back an order form with // hidden fields in it. void Login() throws IOException { // Login request - we expect "name" and "password" String name = request.getParameter("name"); String pass = request.getParameter("password"); if (ValidateUser(name,pass)) { // Valid login - send back order form with hidden // fields out.println("

Hello "); out.println(""+name+", place your order

"); out.println("
\n"); out.println(""); out.println(""); SendFile(path+"orderform.html"); } else { // Invalid login - flame the loser SendFile(path+"baduser.html"); SendFile(path+"loginform.html"); } } // Receipt() sends an HTML receipt back to the browser // Should probably use the Calendar class instead of // Date here... void Receipt(String name, String pizza, String size) throws IOException { Date d = new Date(); // create a string that holds current date SimpleDateFormat f = new SimpleDateFormat("EEEE, MMMM d, yyyy"); String ds = f.format(d); // create a string that holds current time SimpleDateFormat f1 = new SimpleDateFormat("hh:mm a"); String ts = f1.format(d); // pizza will be ready in 45 minutes: Date dout = new Date(d.getTime()+1000*60*45); String ts1 = f1.format(dout); // create a string that holds time pizza is ready out.println("

Thanks for your order "); out.println(name+"

"); out.println("\n"); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); out.println(""); if (size.equals("small")) out.println(""); else if (size.equals("medium")) out.println(""); else out.println(""); out.println("
RECEIPT
Name:"+name+"
Date:"+ds+"
Time:"+ts+"
Pizza:"+pizza+"
Size:"+size+"
Time Ready:"+ts1+"
Amount Due:$6.50
$8.50
$10.50
\n"); } // Send the named file to the browser void SendFile(String filename) throws IOException,FileNotFoundException { BufferedReader f = new BufferedReader(new FileReader(filename)); String line; while ((line=f.readLine())!=null) { out.println(line); } f.close(); } // Method that checks the rinky-dink password file for // the name/password we got boolean ValidateUser(String name,String pass) throws IOException { BufferedReader f = new BufferedReader(new FileReader(path+"pw")); String n,p; while ((n=f.readLine())!=null) { if ((p=f.readLine())!=null) { if ((n.equals(name))&&(p.equals(pass))) { f.close(); return(true); } } else { return(false); } } f.close(); return(false); } // Method that checks to make sure the pizza order is valid. // Here we just have hard-coded constants... // (would be nice to read valid pizza names/sizes from a file) boolean ValidateOrder(String pizza,String size) throws IOException { if ((pizza==null)||(size==null)) { return(false); } if (pizza.equals("Cheese") || pizza.equals("Veggie") || pizza.equals("Pepperoni") || pizza.equals("Sausage")) { // so far so good - check the size if (size.equals("large") || size.equals("medium") || size.equals("small")) { // good order - this is a grat customer! return(true); } } // wise-guy - invalid order return(false); } }