Guest User

Untitled

a guest
Nov 6th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. //data from login form
  2. String username = request.getParameter("username").trim();
  3. String password = request.getParameter("password").trim();
  4.  
  5. SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
  6. Session session = sessionFactory.openSession();
  7. session.beginTransaction();
  8. try {
  9. User currentUser = (User) session.get(User.class, username);
  10. if(password.equals(currentUser.getPassword())) {
  11. response.sendRedirect("index.jsp?page=login&success=true");
  12. } else {
  13. session.getTransaction().rollback();
  14. response.sendRedirect("index.jsp?page=login&success=false");
  15. }
  16. } catch //...
  17.  
  18. HttpSession httpSession = request.getSession();
  19. httpSession.setAttribute("user", user);
  20.  
  21. HttpSession httpSession = request.getSession(false);
  22. //False because we do not want it to create a new session if it does not exist.
  23. User user = null;
  24. if(httpSession != null){
  25. user = (User) httpSession.getAttribute("user");
  26. }
  27.  
  28. if(user!=null){
  29. // Do stuff here
  30. }
  31.  
  32. httpSession.invalidate();
  33.  
  34. HttpSession httpSession = request.getSession();
  35. httpSession.setAttribute("loggedUser", your_user_object reference_here );
  36.  
  37. httpSession.getAttribute("loggedUser");//return type is Object here
Add Comment
Please, Sign In to add comment