0

hello guys i am working on a java web page, i have all my classes and xhtml files. i can create the cookies but i can't check them when the page is loaded. i have a get cookie function which is:

public Cookie getCookie(String name)

it returns null if there is no cookie, i am trying to write

if(user.getCookie==null)
goto login.xhtml

basically that's what i am trying to do. how can i embed this code into html file?

and in my logout, how can i remove all the cookies?

osumatu
  • 410
  • 1
  • 8
  • 25

1 Answers1

0

If you are using JSF 2.x. you can use PreRenderView tag to execute a method before the page is loaded in JSF to check that if the user cookie is not null before rendering the page . The preRenderView event is invoked on every HTTP request. You can make use of the PreRenderViewEvent like below :

<f:metadata>
    <f:event type="preRenderView" listener="#{cookiesChecker.check}" />
</f:metadata>

And here is the listener

@ManagedBean
@RequestScoped 
public class cookiesChecker{

    public void check() {
       if(user.getCookie==null)
       goto login.xhtml
    }

    // ...
}

More Info :

When to use f:viewAction / preRenderView versus PostConstruct?

Community
  • 1
  • 1
Ahmed Gamal
  • 1,666
  • 1
  • 17
  • 25