/* 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("