Advertisement
Guest User

Untitled

a guest
Apr 15th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. /******************************************************************************************************
  2. *
  3. * AdministratorLoginPageServlet.java
  4. *
  5. * Project 2 - CE4208
  6. *
  7. * Group members: Peter Griffin 0746711
  8. * William Walsh 12088447
  9. * Ciaran McKenna 11149221
  10. * Robert English 11149132
  11. *
  12. * Description: This servlet allows an administrator to adjust the quantities of a product in the Product table
  13. * of the database
  14. * The doGet() method displays all the products and their quantities in a table
  15. * The doPost() method uses session bean interfaces to update the database.
  16. *
  17. *
  18. *****************************************************************************************************/
  19. package servletsPackage;
  20.  
  21. import java.io.IOException;
  22. import java.io.PrintWriter;
  23. import javax.servlet.ServletException;
  24. import javax.servlet.http.HttpServlet;
  25. import javax.servlet.http.HttpServletRequest;
  26. import javax.servlet.http.HttpServletResponse;
  27.  
  28. import javax.ejb.*;
  29. import entities.*;
  30. import beans.*;
  31. import javax.ejb.EJB;
  32. import javax.servlet.http.HttpSession;
  33.  
  34. /**
  35. *
  36. * @author puser
  37. */
  38. public class AdministratorLoginPageServlet extends HttpServlet {
  39.  
  40. @EJB
  41. AdministratorBeanLocal administratorBean;
  42.  
  43. /*********************************************************************************************************************
  44. *
  45. * Method: doGet(request, response)
  46. *
  47. * Explanation: Displays username and password input fields and a submit button
  48. * The form's action calls the doPost() method of this servlet.
  49. *
  50. ********************************************************************************************************************/
  51. @Override
  52. protected void doGet(HttpServletRequest request, HttpServletResponse response)
  53. throws ServletException, IOException {
  54.  
  55. response.setContentType("text/html;charset=UTF-8");
  56. PrintWriter out = response.getWriter();
  57. out.println("<html>");
  58. out.println("<head>");
  59. out.println("<title>Administrator Login</title>");
  60. out.println("</head>");
  61. out.println("<body>");
  62. out.println("<h1>Administrator Login</h1>");
  63. out.println("<form action=AdministratorLoginPageServlet method=POST>");
  64. out.println("Username: <input type=text name=username>");
  65. out.println("<br>");
  66. out.println("Password: <input type=password name=password>");
  67. out.println("<input type=submit>");
  68. out.println("</form>");
  69. out.println("</body></html>");
  70.  
  71. }
  72.  
  73. /*********************************************************************************************************************
  74. *
  75. * Method: doPost(request, response)
  76. *
  77. * Explanation: Processes the user input from the doGet() method.
  78. * The characters entered in the input fields in the doGet() method are
  79. * compared with a white list of allowable characters
  80. * A session bean is used to update the session ID of the administrator concerned
  81. * in the Administrator table in the DB
  82. *
  83. ********************************************************************************************************************/
  84. @Override
  85. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  86. throws ServletException, IOException {
  87.  
  88. String usernameEntered = request.getParameter("username");
  89. String passwordEntered = request.getParameter("password");
  90.  
  91. if(usernameEntered.matches("[a-zA-Z0-9]+") && passwordEntered.matches("[a-zA-z0-9!?]+"))
  92. {
  93. // Administrator object = Whatever administrator in the table has the username entered
  94. Administrator a = administratorBean.getAdministrator(request.getParameter("username"));
  95.  
  96. if(a != null)
  97. {
  98. HttpSession session = request.getSession(true);
  99. // if the session is new or the current session ID == that stored on the DB for this administrator
  100. if(session.isNew() || session.getId().equals(a.getSessionId()))
  101. {
  102. if(request.getParameter("password").equals(a.getPassword()))
  103. {
  104. session.setAttribute("loggedIn", new String("true"));
  105. // Set the session_id field of the Administrator table in the DB to the current session ID
  106. administratorBean.changeAdministratorSessionId(a.getId(), session.getId());
  107. response.sendRedirect("AdministratorHomePageServlet");
  108. }
  109. else
  110. {
  111. response.sendRedirect("AdministratorLoginPageServlet");
  112. }
  113. }
  114. else
  115. {
  116. response.sendRedirect("AdministratorLoginPageServlet");
  117. }
  118. }
  119. else
  120. {
  121. response.sendRedirect("AdministratorLoginPageServlet");
  122. }
  123. }
  124. else
  125. {
  126. response.sendRedirect("AdministratorLoginPageServlet");
  127. }
  128.  
  129.  
  130. }
  131.  
  132. /**
  133. * Returns a short description of the servlet.
  134. *
  135. * @return a String containing servlet description
  136. */
  137. @Override
  138. public String getServletInfo() {
  139. return "Short description";
  140. }// </editor-fold>
  141.  
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement