Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8289555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:32:05+00:00 2026-06-08T12:32:05+00:00

This question is related to another one that I posted recently: Understanding HttpServletRequest and

  • 0

This question is related to another one that I posted recently: Understanding HttpServletRequest and cookies in JSF .

In order to implement a Remember Me login in JSF, I am using a cookie and reading it in WebFilter. The filter gets the cookie and sets the cookie value in the SessionScoped ManagedBean, but for some reason the ManagedBean isn’t able to display it in the web page.

Filter’s doFilter implementation:

@Override
public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain)
        throws IOException, ServletException {

    HttpServletRequest req = (HttpServletRequest) request;
    Cookie[] cookies = req.getCookies();
    if (cookies != null) {
        for (Cookie cookie : cookies) {
            if (cookie.getName().equals("MyTestCookie")) {
                System.out.println("Filter got cookie: " + cookie.getValue());
                cookieBean.setValue(cookie.getValue());
            }
        }
    }
    chain.doFilter(request, response);
}

CookieBean class:

@ManagedBean
@SessionScoped
public class CookieBean implements Serializable {

    private String value;

    @PostConstruct
    public void init() {
        System.out.println("Instantiated CookieBean");
    }

    public String getValue() {
        System.out.println("CookieBean returning Value: " + value);
        return value;
    }

    public void setValue(String value) {
        System.out.println("CookieBean getting Value: " + value);
        this.value = value;
    }

    public void create() {
        ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
        Map<String, Object> props = new HashMap<String, Object>();
        props.put("maxAge", 10000);
        ec.addResponseCookie("MyTestCookie", "Hello Cookie", props);
    }    
}

CookieBean cookieBean is injected into the filter by means of javax.inject.Inject annotation.

Body of index.xhtml:

<h:form>
    <h:commandButton value="Create Cookie!" action="#{cookieBean.create()}" >
        <f:ajax render="@form" />
    </h:commandButton>
    <p></p>
    <h:outputText value="Cookie value: #{cookieBean.value}" /> 
</h:form>

The first problem is that, after setting the cookie, if I start a new session (by opening a new browser session), the web page isn’t aware of the cookie value, because the SessionScoped ManagedBean is updated after the page is displayed.

Question 1: how can I detect the cookie value in time to update some rendered attribute in the web page?

The second problem is that, if I reload the web page by pressing the reload (or refresh) button in the browser, the ManagedBean instance is the same as before (the @PostConstruct method isn’t fired), but the web page shows a null cookie value and the same is shown in the server’s output:

CookieBean returning Value: null
Filter got cookie: Hello Cookie
CookieBean getting Value: Hello Cookie

Question 2: how is it possible that a SessionScoped ManagedBean loses its property without being re-created?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T12:32:07+00:00Added an answer on June 8, 2026 at 12:32 pm

    Answer to Question 1: as explained in the accepted answer for this question, I simply had to do a redirect in the backing bean. This forces JSF to produce a new GET request with the cookie in. Supposing that the requesting page was my welcome page, I had to write this in my cookieBean:

    FacesContext.getCurrentInstance().getExternalContext().redirect("faces/index.xhtml");
    

    Well, this works nicely, but the complete URL is now shown in the address bar. I’ll investigate further if there is a way for avoiding this.

    Answer to Question 2: the SessionScoped cookieBean was effectively instantiated twice, one during deployment, and one by the filter. The problem is that I was using a JSF ManagedBean and injecting it with javax.inject.Inject annotation. I have made it a CDI Bean by means of javax.inject.Named and javax.enterprise.context.SessionScoped annotations, and the injection is now working well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question is related to another one I've posted recently: Check printing with Java/JSP
Another question related to this one . I have a List<SortableObjects> that is the
(This question is related to another one , but different enough that I think
This is a question related to another one I asked that was specific to
This question is related to another question I just posted . I'm prepping for
The current top-voted to this question states: Another one that's not so much a
This is kind of related to another question I posted : What are my
Related question: Moving repository trunk to another’s branch (with history) I know that one
This is an offshoot question that's related to another I asked here . I'm
This question is related to another question I wrote: Trouble using DOTNET from PHP.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.