JSF updating page correctly after logging out

Posted on 2010-11-22
Last Modified: 2013-11-24
I have the following method that logs out the user:
public String logout() {
        FacesContext context = FacesContext.getCurrentInstance();
        ExternalContext ec = context.getExternalContext();
        final HttpServletRequest request = (HttpServletRequest)ec.getRequest();
        return "success";
When success is returned, the user is redirected to login.xhtml vi faces-config.xml
The user is logged out and can't navigate where he needs to be authenticated. But the controls that are to be shown only if the user is logged in, are still showing on the first page display. After refreshing the page, or trying to navigate away from it(wich in turn re-displays it) the controls for a logged in user are correctly taken away.

As an example of such an control:
<h:outputText value="#{msgs.loginBoxInfo}#{': '}" rendered="#{bean.isLoggedIn()}" styleClass="boxedTitle"/>

    public boolean isLoggedIn() {
        ExternalContext external = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest) external.getRequest();
        username = request.getRemoteUser();
        if(username == null || username.equals("")) return false; //No user logged in
        if(dbOversikt == null) dbOversikt = new DbOversikt(username); //Making sure that the class handling the database transactions is initiated
        if(dbOversikt != null && subjects == null) subjects = dbOversikt.getSubjects(); //Making sure subjects are fetched from database, if they are not already
        return true;   //Yes, a user is logged in

So, everything works correctly, just one display later then it should. I want all controls as the one mentioned above gone as soon as the user logs out. Any tips?
Question by:itnifl
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 34194498
In your logout method, you probably need to set the username to null or "".
LVL 10

Expert Comment

ID: 34195384
You can try getting session in the isLoggedIn() method.

If it is null, the user is logged out.
If not, call session.getCreationTime(). If it throws an IllegalStateException, the session is invalidated and the user is logged out. Otherwise proceed as you currently do.

Author Comment

ID: 34199184
geof: I changed the logout method and set the username = ""; at the start of the merhod. It didn't help. It looks like the login page that I am directing to is getting rendered before the changes in the logout method occur/are registered.

Hegemon: Could you give me an example?
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 10

Expert Comment

ID: 34199364
public boolean isLoggedIn() {
        ExternalContext external = FacesContext.getCurrentInstance().getExternalContext();
        HttpServletRequest request = (HttpServletRequest) external.getRequest();
        HttpSession session = request.getSession(false);
        if (session == null) return false;
        try {
            session.getCreationTime();      // Or any other call that will throw this exception
        } catch (IllegalStateException e) { // Thrown if the session is invalidated
           return false;

Author Comment

ID: 34202725
Still same problem :(
LVL 22

Accepted Solution

mbonaci earned 500 total points
ID: 34217007
Why don't you use request.isRequestedSessionIdValid() in isLoggedIn(), before getting username?

Author Closing Comment

ID: 34218704
That did the trick! Thank you!

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This video teaches viewers about errors in exception handling.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question