import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;
/**
* The Servlet LoginHandler receives client's data (username and password) from an
* http request. Checks data for validation, if data are valid it forwards the data to the
* viewResults.jsp, otherwise it forwards the data to the errorPrinter.jsp.
*
* @author Sofoklis Stouraitis
*/
public class LoginHandler 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;
/*
* Dispatchers
*/
RequestDispatcher errorDispatcher = getServletContext().getRequestDispatcher("/errorPrinter.jsp");
RequestDispatcher passDispatcher = getServletContext().getRequestDispatcher("/viewResults.jsp");
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 errorPrinter.jsp to print
* it.
*/
errorDispatcher.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());
eloi.close();
/*
* Forwards the request to the errorPrinter.jsp
*/
errorDispatcher.forward(request, response);
return;
}
/*
* Add the ResultSet object in request.
*/
request.setAttribute("records", rs1);
/*
* Forwards request to the viewResults.jsp
*/
passDispatcher.forward(request, response);
/*
* 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 errorPrinter.jsp
*/
errorDispatcher.forward(request, response);
}
}
/**
* Handles HTTP GET 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 doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
doPost(request, response);
}
}//end of class
|