Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //data from login form
- String username = request.getParameter("username").trim();
- String password = request.getParameter("password").trim();
- SessionFactory sessionFactory = new Configuration().configure().buildSessionFactory();
- Session session = sessionFactory.openSession();
- session.beginTransaction();
- try {
- User currentUser = (User) session.get(User.class, username);
- if(password.equals(currentUser.getPassword())) {
- response.sendRedirect("index.jsp?page=login&success=true");
- } else {
- session.getTransaction().rollback();
- response.sendRedirect("index.jsp?page=login&success=false");
- }
- } catch //...
- HttpSession httpSession = request.getSession();
- httpSession.setAttribute("user", user);
- HttpSession httpSession = request.getSession(false);
- //False because we do not want it to create a new session if it does not exist.
- User user = null;
- if(httpSession != null){
- user = (User) httpSession.getAttribute("user");
- }
- if(user!=null){
- // Do stuff here
- }
- httpSession.invalidate();
- HttpSession httpSession = request.getSession();
- httpSession.setAttribute("loggedUser", your_user_object reference_here );
- httpSession.getAttribute("loggedUser");//return type is Object here
Add Comment
Please, Sign In to add comment