xPages Scoped Variables

There are 4 types of scoped variables -  applicationScope,sessionScope,requestScope,viewScope.
Below is the description of each scoped variable. 

applicationScope:

This variable keeps the server memory to hold a variable value saved in server memory so we can use this variable value across all the databases on hosted server.

Every login user can access this variable. This variable can be used as shared variable across all the databases on same server.


Suppose there are 10 applications running on the server and you wrote a method to set applicationScope variable on test xPage lets say - "demoXpage.xsp" as "currency" then you are able to access this variable on all 10 databases hosted on the same server through the xpage.


Belwo is the example of applicationScop variable-
applicationScope.CurrenyName(“USD”) -


Use simple context property to this variable
context.getProperty(“applicationScope.CurrenyName”)

Another way you can use application scope -


applicationScope.put(<variable name>, <value>)

applicationScope.get(<variable name>)

Example:


applicationScope.put(“CurrentCompanyName”,”IBM”)

applicationScope.get(“CurrentCompanyName”)

You can use this CurrentCompanyName variable across all the xpages on the same server. Also, instead of “CurrentCompanyName” variable you can use numeric value, here numeric value can be an object, we don’t need to assign double quotes/string to numeric value



Example :
applicationScope.put(2015,“Current year is 2015”)

applicationScope.get(2015)

How to reset
applicationScope variable values from server memory ?
You can assign another variable value to the same variable
Also , the HTTP restart task will be helpful  to reset the variable value.


SessionScope: 


This variable uses the application current session memory/ or login user current session memory to keep variable value saved for opening session. So this can be used only for current session.


Each login user  will have their own sessionScope variable value  across all xPages. This value can not be shared between user to user but it can be used as shared variable for login user among the all xpages. 


Remember, if you closed the
browser  / or you signed-off from the application then you wont be able to access  to access this varible in next session / next sign-in.

Example:


User “David Cowley” is accessing his session and he is from IBM . So we use bellow syntax to put and to get values on all xpages. We can configure lookup “currentCompanyName” as IBM and then assign to session variable so the next time we don’t need a lookup for remaining xpages for opening session. 

We can access his company name using “get” method as below.

sessionScope.put(“CurrentCompanyName”,”IBM”)

sessionScope.get(“CurrentCompanyName”)


requestScope:


This is a request dependent variable used for one time. I.e. one request and one response.

Send one request from one xpage and get one response to another xpage with same request scope variable name.

requestScope variable will not keep extra server memory.


Example.


xPage 1: QuotationDetails.xsp

requestScope.put("ConfirmQuotation ", "I can confirm that quotation is approved");;
context.redirectToPage("QuotationValidate.xsp");

xPage 2: QuotationValidate.xsp

requestScope.get("ConfirmQuotation");


So this “ConfirmQuotation” key will receive the confirmation message.


Output on xPage 2: “I can confirm that quotation is approved”


Use this variable as per your logic on computed field or anywhere on the page.



ViewScope: 


This variable useful on current opened view on xPage. You can use this to set and get value on the same xpage view.


Its onetime variable for the current xPage view.i.e. current opened browser page For example, if we use search query on view and then we go for another search on same view, then the last search should be destroyed. So we use a viewScope variable for each search query, because last search query should be destroyed from server memory.


Example:


var date:NotesDateTime = database.getCreated();

viewScope.put(“SearchMonth”, date.getGMTTime())


and access this date on your search query also if you want current view name on xPage use

var viewName= viewScope.ViewName

Use this "viewname" on your search query as well.




Summary: 


For all 4 types of scoped variables-

ApplicationScope: Variable used on application levels, i.e. all databases on the server.


sessionScope : Variable used on all xPages on current opened session.


reuestScope : Onetime request from one instance to another instance on xPages


viewScope : Variable used for current view on xPage


Thanks for reading this article, hope you like it, if you have any difficulty please contact us on ‘admin@xpagedomino.com’ or click here to contact us.


3 comments :

Post a Comment

Leave A Comment...