Solved

JSF updating page correctly after logging out

Posted on 2010-11-22
9
854 Views
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();
        request.getSession(false).invalidate();
        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?
0
Comment
Question by:itnifl
9 Comments
 
LVL 6

Expert Comment

by:__geof__
Comment Utility
In your logout method, you probably need to set the username to null or "".
0
 
LVL 10

Expert Comment

by:Hegemon
Comment Utility
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.
0
 
LVL 2

Author Comment

by:itnifl
Comment Utility
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?
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 10

Expert Comment

by:Hegemon
Comment Utility
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;
        }
0
 
LVL 2

Author Comment

by:itnifl
Comment Utility
Thanks.
Still same problem :(
0
 
LVL 22

Accepted Solution

by:
mbonaci earned 500 total points
Comment Utility
Why don't you use request.isRequestedSessionIdValid() in isLoggedIn(), before getting username?
0
 
LVL 2

Author Closing Comment

by:itnifl
Comment Utility
That did the trick! Thank you!
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now