Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You don't need to. It makes in well designed webapps no sense. The servletcontainer does already the session management. Just put the logged-in user in the session scope.</p> <pre><code>@ManagedBean @RequestScoped public class LoginController { private String username; private String password; @EJB private UserService userService; public String login() { User user = userService.find(username, password); FacesContext context = FacesContext.getCurrentInstance(); if (user == null) { context.addMessage(null, new FacesMessage("Unknown login, try again")); username = null; password = null; return null; } else { context.getExternalContext().getSessionMap().put("user", user); return "userhome?faces-redirect=true"; } } public String logout() { FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); return "index?faces-redirect=true"; } // ... } </code></pre> <p>The logged-in user will be available as <code>#{user}</code> in all pages throughout the same session and also in <code>@ManagedProperty</code> of other beans.</p> <p>On logout, however, it makes more sense to invalidate the session. This will trash all session scoped beans as well. You can use <a href="http://download.oracle.com/javaee/6/api/javax/faces/context/ExternalContext.html#invalidateSession%28%29" rel="nofollow noreferrer"><code>ExternalContext#invalidateSession()</code></a> for this.</p> <pre><code> public String logout() { FacesContext.getCurrentInstance().getExternalContext().invalidateSession(); return "index?faces-redirect=true"; } </code></pre> <h3>See also:</h3> <ul> <li><a href="https://stackoverflow.com/questions/3106452/how-do-servlets-work-instantiation-shared-variables-and-multithreading/">How do servlets work? Instantiation, sessions, shared variables and multithreading</a></li> <li><a href="https://stackoverflow.com/questions/9965708/how-to-handle-authentication-authorization-with-users-in-a-database">How to handle authentication/authorization with users in a database?</a></li> <li><a href="https://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope/">How to choose the right bean scope?</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload