Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************************************
- *
- * AdministratorLoginPageServlet.java
- *
- * Project 2 - CE4208
- *
- * Group members: Peter Griffin 0746711
- * William Walsh 12088447
- * Ciaran McKenna 11149221
- * Robert English 11149132
- *
- * Description: This servlet allows an administrator to adjust the quantities of a product in the Product table
- * of the database
- * The doGet() method displays all the products and their quantities in a table
- * The doPost() method uses session bean interfaces to update the database.
- *
- *
- *****************************************************************************************************/
- package servletsPackage;
- import java.io.IOException;
- import java.io.PrintWriter;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import javax.ejb.*;
- import entities.*;
- import beans.*;
- import javax.ejb.EJB;
- import javax.servlet.http.HttpSession;
- /**
- *
- * @author puser
- */
- public class AdministratorLoginPageServlet extends HttpServlet {
- @EJB
- AdministratorBeanLocal administratorBean;
- /*********************************************************************************************************************
- *
- * Method: doGet(request, response)
- *
- * Explanation: Displays username and password input fields and a submit button
- * The form's action calls the doPost() method of this servlet.
- *
- ********************************************************************************************************************/
- @Override
- protected void doGet(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- response.setContentType("text/html;charset=UTF-8");
- PrintWriter out = response.getWriter();
- out.println("<html>");
- out.println("<head>");
- out.println("<title>Administrator Login</title>");
- out.println("</head>");
- out.println("<body>");
- out.println("<h1>Administrator Login</h1>");
- out.println("<form action=AdministratorLoginPageServlet method=POST>");
- out.println("Username: <input type=text name=username>");
- out.println("<br>");
- out.println("Password: <input type=password name=password>");
- out.println("<input type=submit>");
- out.println("</form>");
- out.println("</body></html>");
- }
- /*********************************************************************************************************************
- *
- * Method: doPost(request, response)
- *
- * Explanation: Processes the user input from the doGet() method.
- * The characters entered in the input fields in the doGet() method are
- * compared with a white list of allowable characters
- * A session bean is used to update the session ID of the administrator concerned
- * in the Administrator table in the DB
- *
- ********************************************************************************************************************/
- @Override
- protected void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- String usernameEntered = request.getParameter("username");
- String passwordEntered = request.getParameter("password");
- if(usernameEntered.matches("[a-zA-Z0-9]+") && passwordEntered.matches("[a-zA-z0-9!?]+"))
- {
- // Administrator object = Whatever administrator in the table has the username entered
- Administrator a = administratorBean.getAdministrator(request.getParameter("username"));
- if(a != null)
- {
- HttpSession session = request.getSession(true);
- // if the session is new or the current session ID == that stored on the DB for this administrator
- if(session.isNew() || session.getId().equals(a.getSessionId()))
- {
- if(request.getParameter("password").equals(a.getPassword()))
- {
- session.setAttribute("loggedIn", new String("true"));
- // Set the session_id field of the Administrator table in the DB to the current session ID
- administratorBean.changeAdministratorSessionId(a.getId(), session.getId());
- response.sendRedirect("AdministratorHomePageServlet");
- }
- else
- {
- response.sendRedirect("AdministratorLoginPageServlet");
- }
- }
- else
- {
- response.sendRedirect("AdministratorLoginPageServlet");
- }
- }
- else
- {
- response.sendRedirect("AdministratorLoginPageServlet");
- }
- }
- else
- {
- response.sendRedirect("AdministratorLoginPageServlet");
- }
- }
- /**
- * Returns a short description of the servlet.
- *
- * @return a String containing servlet description
- */
- @Override
- public String getServletInfo() {
- return "Short description";
- }// </editor-fold>
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement