Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

JSF updating page correctly after logging out

Posted on 2010-11-22
9
Medium Priority
?
864 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
7 Comments
 
LVL 6

Expert Comment

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

Expert Comment

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

Author Comment

by:itnifl
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?
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 10

Expert Comment

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

Author Comment

by:itnifl
ID: 34202725
Thanks.
Still same problem :(
0
 
LVL 22

Accepted Solution

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

Author Closing Comment

by:itnifl
ID: 34218704
That did the trick! Thank you!
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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 …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

782 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