Understanding scopes and managed beans

Source: Internet
Author: User
Tags terminates glassfish

The WEB application that you create in this tutorial uses an object in the application scope to count the votes, using an object within the session scope to ensure that the user can vote only once per conversation. The application uses a request-scoped object to display the time the user submitted the poll. The time is stored in the request scope because the application no longer needs this value after the response is sent to the client browser.

This tutorial requires the following technologies and resources

JavaServer Faces Components/

Java EE Platform

1.2 and Java EE 5*

1.1 and Java EE 1.4

Travel Database Not necessary

* To take advantage of the Java EE 5 capabilities of NetBeans IDE 6.0, use an application server that fully conforms to the Java EE 5 specification, such as the Sun Java application Server 9/glassfish.

This tutorial has been adjusted for applications on the GlassFish V2 application server. If you are using a different server, see the release notes and frequently asked questions to learn about known issues and workarounds. For information about supported servers and the Java EE platform, refer to the release notes.

About Scopes

When a user stays on a page, the value of the component is stored even if the page is displayed again, such as when the user clicks on a button that returns a null value. However, when the user leaves the page, the value of the component disappears.

To make the value available to other pages, or to the same page that the user should return, you need to store the value. When you create a project from the IDE, the IDE creates three managed beans to store the values:

RequestBean1

SessionBean1

ApplicationBean1

The following illustration shows a navigator window that lists the managed beans.

Figure 1: Default managed Bean

A managed Bean is a JavaBean object that is rendered by the JavaServer Faces Web application and stored in the request, session, or application scope. The Web application stores the RequestBean1 in the request scope, stores the SessionBean1 in the session scope, and stores the ApplicationBean1 in application scope.

To add properties to these managed beans, you can right-click a Bean in the Navigator form and type a property declaration in the source code. Right-click anywhere in the Java editor, and choose Refactor > Encapsulate fields. Then select the attribute from the list and tick the method you want to add, and click "Construct".

Before you can create a bean property to store a value, you must determine the applicable scope of the property value. Because many users may access the Web application at the same time, you need to use the smallest possible scope to make the best use of server resources. The following figure shows the duration for each scope.

The application scope persists until the server stops the application. The values that you store in the application bean are valid for each session and each request that uses the same application mapping.

The session scope begins when a user accesses a page in a Web application for the first time, when a user's session times out because of a long inactivity, or when the Web application makes the session invalid, such as by calling Session.invalidate (), the conversation terminates.

The Request scope starts when the user submits the page and terminates when the response is complete, regardless of what page is returned.

Chart 2:web Application scope

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.