import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;

// This servlet demonstrates how we can forward information to
// other servlets, and include the output of other servlets in
// responses. This servlet acts as a login servlet, which checks
// for a correct password before the rest of the request is processed.

public class LoginServlet extends HttpServlet {

  public void doPost(HttpServletRequest request, 
                    HttpServletResponse response) 
                   throws ServletException, IOException {

    PrintWriter out = response.getWriter(); 
    
    // Get the name and password entered. Normally, we would
    // check this against a database, but for now we will just
    // check whether the password is a particular value.

    String name = request.getParameter("name");  
    String password = request.getParameter("password");

    if (password.equals("12345")) {

      // The password is correct, so forward the request to the
      // existing ReservarionServlet. Note that the structure
      // of the call assumes that the ReservationServlet is in 
      // the same directory of the web container as this one.

      RequestDispatcher correctLogin = 
         request.getRequestDispatcher("ReservationServlet1");
      correctLogin.forward(request, response);

      }

    else {

      // Otherwise, we send an error message, and include the
      // original login page so the user can try again.

      out.println("<HTML><BODY><P><B>INCORRECT PASSWORD!!</B></P>");

      RequestDispatcher badLogin = 
         request.getRequestDispatcher("reservation.htm");
      badLogin.include(request, response);

      }   
    }

  }
