import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

/* This class acts as a simple reservation system (which doesn't
   actually reserve anything) in order to demo how form data can be
   processed and a response page can be generated.
*/

// We extend the HttpServlet class, which contains all of the methods
// necessary to respond to either POST or GET methods of sending
// form data.

public class ReservationServlet extends HttpServlet {

  // Since our web page form that will submit the data does so using
  // the POST method, we implement doPost (we would implement doGet
  // if the GET method were used). This is passed objects containing
  // request data sent from the form, and response data which will be
  // sent back to the browser. Note that it should always throw a
  // ServletException as well (this is a non-runtime exception).

  public void doPost(HttpServletRequest request, 
                    HttpServletResponse response) 
                   throws ServletException, IOException {

    // The response object is like a file in the sense that we can
    // create a PrintWriter object to send String data into the
    // object (this string will be the web page that will be sent
    // back to the browser). Once we have created a PrintWriter that
    // is connected to the response, we can use println to send the
    // data into it.

    PrintWriter out = response.getWriter(); 
    
    // The request object contains data about the requester other than
    // just the form data. Commonly used data includes the hostname
    // and host address of the requestor, among others.

    String remoteHost = request.getRemoteHost();
    String remoteAddr = request.getRemoteAddr();

    // Each piece of form data sent is in "name=value" form, where the
    // name is that given in the HTML of the form, and the value is
    // whatever the user entered. Each of these can be retrieved using
    // the getParameter method.

    // For text elements, the value is whatever the user typed into the box

    String name = request.getParameter("name");  
    String course = request.getParameter("course");
    String password = request.getParameter("password");

    // For checkboxes, the value is either "on" if the checkbox was checked,
    // or null if it was not checked. CHecking for null is the simplest
    // way to get the value.

    String office = request.getParameter("Office");
    String studio = request.getParameter("Studio");
    String java = request.getParameter("Java");
    String adobe = request.getParameter("Adobe");

    // For radio buttons and single option lists, the value is 
    // that attached to the button selected,
    // or null if none are selected (you should check for this!)

    String room = request.getParameter("room");
    if (room == null) {
      out.println("Request could not be processed -- room missing!");
      return;
      }
    String time = request.getParameter("time");
    String why = request.getParameter("why");

    // If a multiple-option list is used, the request object contains
    // an array of values corresponding to all options selected, or
    // null if none were selected.

    String dayString = "";
    String[] days = request.getParameterValues("days"); 
    if (days == null) {
      out.println("Request could not be processed -- days missing!");
      return;
      } 
 
    int dayLength = days.length;
    if (dayLength == 1) dayString = days[0];
    else if (dayLength == 2) dayString = days[0] + " and " + days[1];
    else {
      for (int i = 0; i < dayLength - 1; i++)
        dayString = dayString + days[i] + ", ";
      dayString = dayString + " and " + days[dayLength - 1];
      }

    // To send a response page to the browser, we write strings corresponing
    // to a page in HTML to the response object.

    out.println("<HTML><HEAD><TITLE>Response to reservation</TITLE></HEAD><BODY>");
    out.println("<P>Thank you, " + name);
    out.println(" on " + remoteHost + " at " + remoteAddr + "</P>");
    out.println("<P>This is to acknowledge that you have reserved room " + room);
    out.println(" at " + time + " on " + dayString + "</P>");
    out.println("<P>The following software will be available: ");
    if (office != null) out.println("<BR>Microsoft Office");
    if (studio != null) out.println("<BR>Visual Studio");
    if (java != null) out.println("<BR>Java SDK");
    if (adobe != null) out.println("<BR>Adobe Acrobat");
    out.println("</P>");
    out.println("<P>Other comments: " + why + "</P>");
    out.println("</BODY></HTML>");
    
    }

  }
