Advertisement
Guest User

Untitled

a guest
Apr 10th, 2018
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.55 KB | None | 0 0
  1. /*
  2. * AA 2017-2018
  3. * Introduction to Web Programming
  4. * Lab 07 - ShoppingList List
  5. * UniTN
  6. */
  7. package it.unitn.aa1718.webprogramming.lab07.shoppingList.servlets;
  8.  
  9. import it.unitn.aa1718.webprogramming.lab07.shoppingList.db.daos.UserDAO;
  10. import it.unitn.aa1718.webprogramming.lab07.shoppingList.db.entities.User;
  11. import it.unitn.aa1718.webprogramming.lab07.shoppingList.db.exceptions.DAOException;
  12. import it.unitn.aa1718.webprogramming.lab07.shoppingList.db.exceptions.DAOFactoryException;
  13. import it.unitn.aa1718.webprogramming.lab07.shoppingList.db.factories.DAOFactory;
  14. import java.io.IOException;
  15. import java.io.PrintWriter;
  16. import javax.servlet.ServletException;
  17. import javax.servlet.http.HttpServlet;
  18. import javax.servlet.http.HttpServletRequest;
  19. import javax.servlet.http.HttpServletResponse;
  20.  
  21. /**
  22. * Servlet that handles the login web page.
  23. *
  24. * @author Stefano Chirico <stefano dot chirico at unitn dot it>
  25. * @since 2018.04.04
  26. */
  27. public class LoginServlet extends HttpServlet {
  28.  
  29. private UserDAO userDao;
  30.  
  31. @Override
  32. public void init() throws ServletException {
  33. DAOFactory daoFactory = (DAOFactory) super.getServletContext().getAttribute("daoFactory");
  34. if (daoFactory == null) {
  35. throw new ServletException("Impossible to get dao factory for user storage system");
  36. }
  37. try {
  38. userDao = daoFactory.getDAO(UserDAO.class);
  39. } catch (DAOFactoryException ex) {
  40. throw new ServletException("Impossible to get dao factory for user storage system", ex);
  41. }
  42. }
  43.  
  44. /**
  45. * Handles the HTTP <code>GET</code> method.
  46. *
  47. * @param request servlet request
  48. * @param response servlet response
  49. * @throws ServletException if a servlet-specific error occurs
  50. * @throws IOException if an I/O error occurs
  51. *
  52. * @author Stefano Chirico
  53. * @since 1.0.180404
  54. */
  55. @Override
  56. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  57. String contextPath = getServletContext().getContextPath();
  58. if (!contextPath.endsWith("/")) {
  59. contextPath += "/";
  60. }
  61.  
  62. response.setContentType("text/html");
  63. PrintWriter out = response.getWriter();
  64. out.println(
  65. "<!DOCTYPE html>\n"
  66. + "<html>\n"
  67. + " <head>\n"
  68. + " <title>Lab 07: Authentication Area</title>\n"
  69. + " <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"
  70. + " <meta name=\"viewport\" content=\"width=device-width, initial-scale=1, shrink-to-fit=no\">\n"
  71. + " <!-- Latest compiled and minified CSS -->\n"
  72. + " <link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css\" crossorigin=\"anonymous\">\n"
  73. + " <link rel=\"stylesheet\" href=\"https://use.fontawesome.com/releases/v5.0.8/css/all.css\" crossorigin=\"anonymous\">\n"
  74. + " <!-- Custom styles for this template -->\n"
  75. //+ " <link href=\"css/signin.css\" rel=\"stylesheet\">\n"
  76. + " <link href=\"css/floating-labels.css\" rel=\"stylesheet\">\n"
  77. + " </head>\n"
  78. + " <body>\n"
  79. // + " <div class=\"container\">\n"
  80. // + " <div class=\"jumbotron\">\n"
  81. + " <form class=\"form-signin\" action=\"" + contextPath + "login.handler\" method=\"POST\">\n"
  82. + " <div class=\"text-center mb-4\">\n"
  83. + " <img class=\"mb-4\" src=\"images/unitn_logo_1024.png\" width=\"128\" height=\"128\">\n"
  84. + " <h3 class=\"h3 mb-3 font-weight-normal\">Authentication Area</h3>\n"
  85. + " <p>You must authenticate to access, view, modify and share your Shopping Lists</p>\n"
  86. + " </div>\n"
  87. + " <div class=\"form-label-group\">\n"
  88. + " <input type=\"email\" id=\"username\" name=\"username\" class=\"form-control\" placeholder=\"Username\" required autofocus>\n"
  89. + " <label for=\"username\">Username</label>\n"
  90. + " </div>\n"
  91. + " <div class=\"form-label-group\">\n"
  92. + " <input type=\"password\" id=\"password\" name=\"password\" class=\"form-control\" placeholder=\"Password\" required>\n"
  93. + " <label for=\"password\">Password</label>\n"
  94. + " </div>\n"
  95. + " <div class=\"checkbox mb-3\">\n"
  96. + " <label>\n"
  97. + " <input type=\"checkbox\" name=\"rememberMe\" value=\"true\"> Remember me\n"
  98. + " </label>\n"
  99. + " </div>\n"
  100. + " <button class=\"btn btn-lg btn-primary btn-block\" type=\"submit\">Sign in</button>\n"
  101. + " </form>\n"
  102. // + " </div>\n"
  103. // + " </div> <!-- /container -->\n"
  104. + " <!-- Latest compiled and minified JavaScript -->\n"
  105. + " <script src=\"https://code.jquery.com/jquery-3.2.1.min.js\" crossorigin=\"anonymous\"></script>\n"
  106. + " <script src=\"https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js\" crossorigin=\"anonymous\"></script>\n"
  107. + " <script src=\"https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js\" crossorigin=\"anonymous\"></script>\n"
  108. + " </body>\n"
  109. + "</html>"
  110. );
  111. }
  112.  
  113. /**
  114. * Handles the HTTP <code>POST</code> method.
  115. *
  116. * @param request servlet request
  117. * @param response servlet response
  118. * @throws ServletException if a servlet-specific error occurs
  119. * @throws IOException if an I/O error occurs
  120. *
  121. * @author Stefano Chirico
  122. * @since 1.0.180404
  123. */
  124. @Override
  125. protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  126. String email = request.getParameter("username");
  127. String password = request.getParameter("password");
  128.  
  129. String contextPath = getServletContext().getContextPath();
  130. if (!contextPath.endsWith("/")) {
  131. contextPath += "/";
  132. }
  133.  
  134. try {
  135. User user = userDao.getByEmailAndPassword(email, password);
  136. if (user == null) {
  137. response.sendRedirect(response.encodeRedirectURL(contextPath + "login.handler"));
  138. } else {
  139. request.getSession().setAttribute("user", user);
  140. if (user.getEmail().equals("stefano.chirico@unitn.it")) {
  141. response.sendRedirect(response.encodeRedirectURL(contextPath + "restricted/users.handler"));
  142. } else {
  143. response.sendRedirect(response.encodeRedirectURL(contextPath + "restricted/shopping.lists.handler?id=" + user.getId()));
  144. }
  145. }
  146. } catch (DAOException ex) {
  147. //TODO: log exception
  148. request.getServletContext().log("Impossible to retrieve the user", ex);
  149. }
  150. }
  151. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement