/* Java Servlet example - Netprog Pizza Server Uses a HttpSession to track the user */ import java.io.*; import java.text.*; import java.util.*; import javax.servlet.*; import javax.servlet.http.*; public class PizzaSession extends HttpServlet { HttpServletRequest request; HttpServletResponse response; PrintWriter out; HttpSession session; static String path="c:\\jdk1.3\\tomcat\\webapps\\Root\\PizzaSession\\"; // 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 { // get any session info, or create one if there is none. session = request.getSession(true); // 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 // check for an ongoing session if (form==null) { // First attempt a session based automatic login if (! SessionLogin()) { // no valid session - send login form 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 to get a sesion variable // named "pizzasession" // Check before worrying about the pizza and size String name=getNameSession(); // Validate is based on name only // we got the password earlier // In "real life" this needs to be more robust, perhaps // using a session key if (ValidateName(name)) { 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 // and create a session 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 - set session variable to remember username session.setAttribute("pizzasession",name); // Send back message and the order form out.println("
| RECEIPT | |
|---|---|
| Name: | "+name+" |
| Date: | "); out.println(""+ds+" |
| Time: | "); out.println(""+ts+" |
| Pizza: | "); out.println(""+pizza+" |
| Size: | "); out.println(""+size+" | Time Ready: | "); out.println(""+ts1+" | "); out.println("
| Amount Due: | "); if (size.equals("small")) out.println("$6.50 | $8.50 | "); else out.println("$10.50 | "); out.println("