title: How to store retrieved object in session and access it afterwords?
body: <p>I am trying to create a simple login page. I retrieve a <code>User</code> object from my database using hibernate. That part works fine, I'm doing that as follows:</p>
  
  <pre><code>//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&amp;success=true");
      } else {
          session.getTransaction().rollback();
          response.sendRedirect("index.jsp?page=login&amp;success=false");
      }
  } catch //...
  </code></pre>
  
  <p>Given the correct credentials, login is successful. If I understand correctly, my code above already stores the User in the session, if the login was successful, so all I have to do is access the session?</p>
  
  <p>However I can't figure out how to access the retrieved <code>User</code> object from the session in other places of my website. After the user is logged in, I want to show user-specific information on my website and for that, I need to check the username and whether the user is logged in at all.</p>
  
  <p>So to sum up: How can I use the retrieved <code>User</code> object in other parts of my website?</p>
  
  <p>I just started learning Java EE and hibernate, so please bear with me.</p>