nerdexam
Oracle

1Z0-900 · Question #63

You are building the User Preferences page of an application. A user can change values, such as his or her name, password, address, company, and so on. These values are sent to a CDI backing bean via

B (View) and C (Session) are the correct answers. @ViewScoped is the ideal choice here - the bean lives exactly as long as the user remains on the same JSF view/page, so every AJAX tab-out request shares the same bean instance, accumulating changes until Save is clicked. @Session

Use CDI Beans

Question

You are building the User Preferences page of an application. A user can change values, such as his or her name, password, address, company, and so on. These values are sent to a CDI backing bean via AJAX when the user tabs out of each field. However, the values must be retained in the CDI bean and stored in the database only when the user clicks the Save button. Which two scopes will allow your CDI bean to retain its state while the user is interacting with the User Preferences page? (Choose two.)

Options

  • ADependent
  • BView
  • CSession
  • DRequest
  • EApplication

Explanation

B (View) and C (Session) are the correct answers.

@ViewScoped is the ideal choice here - the bean lives exactly as long as the user remains on the same JSF view/page, so every AJAX tab-out request shares the same bean instance, accumulating changes until Save is clicked. @SessionScoped also works because the bean survives for the entire user session, which naturally spans the time the user spends on the page; however, it's broader than necessary and carries memory overhead.

@RequestScoped (D) is wrong because a new bean instance is created and destroyed with each HTTP request - every AJAX call would start fresh, discarding previously entered values immediately. @Dependent (A) is wrong because the bean inherits the lifecycle of whatever injects it; if that's a request-scoped component, state is still lost per request. @ApplicationScoped (E) is wrong because a single bean instance is shared across all users of the application - storing one user's preferences there would expose or overwrite another user's data.

Memory tip: Think "how long do I need the state?" - Request = one round trip, View = one page stay, Session = one browser visit, Application = forever/everyone. For "stay on this page and save later," View is the minimum scope that works, and Session is the maximum practical one.

Topics

#CDI scopes#state persistence#view scope#session scope

Community Discussion

No community discussion yet for this question.

Full 1Z0-900 Practice