Advertisement
Guest User

Untitled

a guest
Aug 7th, 2017
439
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. protected void doPost(HttpServletRequest request, HttpServletResponse response)
  2. throws ServletException, IOException {
  3. HttpSession httpSession = request.getSession();
  4. Session session = HibernateUtil.getSessionFactory().getCurrentSession();
  5. String username = request.getParameter("username");
  6. String password = request.getParameter("password");
  7. String queryString = "from User where username = :username and password = :password";
  8. String url, message = "";
  9. Query query;
  10.  
  11.  
  12. User user =(User) httpSession.getAttribute("user");
  13. if(user == null){
  14. user = new User();
  15. }
  16.  
  17. try {
  18. session.beginTransaction();
  19.  
  20.  
  21. query = session.createQuery(queryString);
  22. query.setString("password", password);
  23. query.setString("username", username);
  24. Object queryResult = query.uniqueResult();
  25. User tmpUser = (User) queryResult;
  26.  
  27.  
  28.  
  29. if (tmpUser != null) {
  30. user = tmpUser;
  31. url = "/welcome.jsp";
  32. } else {
  33. message = "invalid username or password";
  34. user.addLogAttempt();
  35. if (user.getLoginAttempts() > 2) {
  36. url = "/addUser.jsp";
  37. } else {
  38. url = "/index.jsp";
  39. }
  40. }
  41. httpSession.setAttribute("user", user);
  42. httpSession.setAttribute("message", message);
  43. /* User user = new User();
  44. user.setUsername("ryanggfx2");
  45. user.setEmailAddress("ryanggfx2@gmail.com");
  46. user.setPassword("ryanggfx2");
  47. */
  48. session.getTransaction().commit();
  49.  
  50. } catch (Exception e) {
  51. url = "/error.jsp";
  52. request.setAttribute("error", e);
  53. } finally {
  54. }
  55.  
  56. RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
  57. dispatcher.forward(request, response);
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement