CheckUser.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

/**
 * The Servlet CheckUser receives client's data (username and password) from an
 * http request. Checks data for validation, if data are valid it prints a
 * table with all ELOI staff, otherwise it forwards the data to the
 * ErrorDisplayerServlet Servlet to handle them.
 *
 @author Sofoklis Stouraitis
 */
public class CheckUser extends HttpServlet {

  /**
   * Handles HTTP POST requests.
   *
   @param request
   *            the request object
   @param response
   *            the response object
   *
   @throws IOException
   *             if an input or output error is detected when the servlet
   *             handles the POST request.
   @throws ServletException
   *             if the request for the POST could not be handled.
   */
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws IOException, ServletException {

    response.setContentType("text/html; charset=ISO-8859-7");
    PrintWriter out = new PrintWriter(response.getWriter()true);
    /*
     * Gets parameters from html
     */
    String userName = request.getParameter("USER_NAME");
    String userPassword = request.getParameter("PASSWORD");

    ResultSet rs1 = null;
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/servlet/ErrorDisplayerServlet");

    try {
      /*
       * Initialize EloiStaff Object in order to connect to the database.
       */
      EloiStaff eloi = new EloiStaff();
      /*
       * Establish connection with database.
       */
      eloi.open();

      if (!eloi.isUserValid(userName, userPassword)) {
        /*
         * Ends the connection with database.
         */
        eloi.close();

        /*
         * Puts the error message to the request object.
         */
        request.setAttribute("error", eloi.getErrorMessages());
        /*
         * Forwards the request to the ErrorDisplayerServlet to print
         * it.
         */
        dispatcher.forward(request, response);
        return;
      }
      /*
       * Executes query and gets the results.
       */
      rs1 = eloi.getEloiStaff();

      if (rs1 == null) {
        eloi.close();
        /*
         * Puts the error message to the request object.
         */
        request.setAttribute("error", eloi.getErrorMessages());
        /*
         * Forwards the request to the ErrorDisplayerServlet to print
         * it.
         */
        dispatcher.forward(request, response);
        return;
      }

      out.println("<html>");
      out.println("<head>");
      out.println("<Meta Http-Equiv='Content-Type' Content='text/html; Charset=iso-8859-7'>");
      out.println("<title>Προσωπικό Εκπαιδευτικού Εργαστηρίου Πληροφορικής</title>");
      out.println("</head>");
      out.println("<body>");
      out.println("<br><div align='center'>");
      out.println("<table border='1' width='60%'>");
      out.println("<tr>");
      out.println("<td width='11%' bgcolor='#C0C0C0'>");
      out.println("<p align='center'><b><font color='#000080'>A.M&nbsp;:</font></b></td>");
      out.println("<td width='39%' bgcolor='#C0C0C0'>");
      out.println("<p align='center'><b><font color='#000080'>SURNAME&nbsp;:</font></b></td>");
      out.println("<td width='44%' bgcolor='#C0C0C0'>");
      out.println("<p align='center'><b><font color='#000080'>NAME&nbsp;:</font></b></td>");
      out.println("<td width='6%' bgcolor='#C0C0C0'>"
          "<b><font color='#000080'>R.N&nbsp;:</font></b></td>");
      out.println("</tr>");

      while (rs1.next()) {

        String am = rs1.getString("AM");
        String surname = rs1.getString("SURNAME");
        String name = rs1.getString("NAME");
        int idNumber = rs1.getInt("ID");

        out.println("<tr>");
        out.println("<td width='11%' align='center'>" + am + "</td>");
        out.println("<td width='39%' align='center'>" + surname  + "</td>");
        out.println("<td width='44%' align='center'>" + name + "</td>");
        out.println("<td width='6%' align='center'>" + idNumber  + "</td>");
        out.println("</tr>");
      }

      out.println("</table></div>");
      out.println("</body>");
      out.println("</html>");

      /*
       * ends the connection with database
       */
      eloi.close();

    catch (Exception e) {
      /*
       * puts the error message to the request object.
       */
      request.setAttribute("error", e.getMessage());
      /*
       * Forwards the request to the ErrorDisplayerServlet to print it.
       */
      dispatcher.forward(request, response);
    }
  }
}//end of class