In this we will look at the four different scoped variables, applicationScope, sessionScope, requestScope, and viewScope. We will look at the benefits of using each of the different scopes. We will investigate when to use each of the scopes. The JavaWorld topic on
More JSP best practices
gives a great discussion of each scope.
applicationScope
The applicationScope duration is the WebModule duration. A web module is started when the first request comes in, and is eventually discarded after a period of inactivity, the default being 30 minutes. Every user of the application can access these variables once they are created, so there is no privacy with these variables. The applicationScope should only be used for data that must be shared among many XPages.
sessionScope
With sessionScope variables, the variable lasts for the duration of a server session. This is when the user closes their browser, times out, or when a user logs off. The sessionScope is useful for using you want to store values for the current session only and specific to the current user. Another description of a sessionScope can be found at Java Tips page on
How to use Session Scope
.
requestScope
The requestScope variables last for the duration of the request. This means the variables will last for the duration of a url request from the server. The variable can exist across XPages i.e. if an XPage is requested from another XPage. The requestScope variables are useful for variables which can be deleted straight after the value is submitted to the server. This means that there is not extra memory being used to store variables that are no longer needed.
viewScope
The viewScope is an XPage extension to JSF and it's variables exist for the duration of the current view i.e. the current page. They are not accessible outside of that view. The viewScope is useful when a value must be stored for the current page and can deleted when the user moves to another page.
JavaScript methods
|
|
|
|
|
Returns all the mappings contained in the scope's map.
|
|
|
Returns the keys contained in the scope's map.
|
|
|
Returns true if the scope contains a mapping for the key.
|
|
|
Returns true if the scope contains a mapping for the value.
|
|
|
Returns true if scope key is equal to scope object.
|
|
|
Returns the value that matches the key.
|
|
|
Returns the hashcode for the scope's map or for the scope's key to which it is applied.
|
|
|
Returns true if the scope's map is empty or true when the scope's key to which it is applied is empty.
|
put(Object key, Object value)
|
Places the key with the specified value onto the scope's map. It can be used to replace the value of the matching key, if the key already exists.
|
|
|
Removes the key from the scope's map.
|
|
|
Returns the size of the scope's map i.e. it returns the number of keys in this map.
|
|
|
Returns the values contained in the scope's map.
|
The preferred way to placing a key with a specified value onto the scope's map would be using the dot notation. For example, when we want to create a sessionScope variable called "name" which has "UserName" as it's value you would use:
sessionScope.name = "John Doe";
or similarly,
sessionScope["name"] = "John Doe"
Summary
In this article we have looked at each of the scoped variables. We looked at the duration of each of the scopes and when they may be used. Finally, we looked at all of the JavaScript methods that could be used on the scopes.