Web applications are considered stateless because they do not automatically save information about a user's interaction with the application. If the application needs to remember details about the user's actions, it must explicitly store that information somewhere.
Web applications often store these details in the user's session object, an area of memory on the server that stores temporary information about one user's transactions and interaction with the web application. The data in the session object is not as stable as data stored in a database because the session object is lost if the session ends. Still, putting data in the session object is useful in the short term if done efficiently.
When a user visits an EGL-controlled web page, the server loads the page's JSF Handler, represented at run time as a page bean, stores that bean in the session object as a session variable, and uses that bean to provide the logic for the page. If the scope property of the JSF Handler is set to session and its cancelOnPageTransition property is set to no, the bean remains in the session object until the session ends or the bean times out. In this way, variables in the Handler can retain their values during the session, even if the user moves to another page.
However, page beans can take up large amounts of memory. A much more efficient way to use the session object (and, in turn, server resources) is to store the user's information in your own smaller session variables and then remove the beans from the session object by setting the scope of the JSF Handlers to request or cancelOnPageTransition to yes. In this way, you retain only the data the application needs, not all the variables in its Handlers.
myVar string = "Hello";
J2EELib.setSessionAttr("mySessionVar", myVar);
myVarFromSession string;
J2EELib.getSessionAttr("mySessionVar", myVarFromSession);
package jsfhandlers;
handler sessionPageOne type JSFHandler
{scope = request,
view = "sessionPageOne.jsp"}
userRecord sessionRecord;
function storeAndForward()
J2EELib.setSessionAttr("mySessionRecord",
userRecord);
forward to "sessionPageTwo";
end
end
record sessionRecord type BasicRecord
userName string;
idNumber int;
end
This example assumes a web page named sessionPageOne.jsp with
two input fields bound to the fields in the record, along with a command
button
bound to the storeAndForward function. When the user
clicks
the button bound to the storeAndForward function,
the record
is added to the user's session variable, and the user is forwarded
to another
page, represented by the following JSF Handler:package jsfhandlers;
handler sessionPageTwo type JSFHandler
{view = "sessionPageTwo.jsp",
onPreRenderFunction = onPreRender}
submittedRecord sessionRecord;
function onPreRender()
J2EELib.getSessionAttr("mySessionRecord",
submittedRecord);
end
end
Like the previous Handler, this example assumes that you
have
bound the fields in the record to output variables on the sessionPageTwo.jsp page.
This Handler retrieves the data from the session variable and assigns
it to
a variable for temporary use within the JSF Handler.J2EELib.clearSessionAttr("mySessionRecord");
Also, you can remove all EGL-controlled session variables from the user's session object with the J2EELib.clearEGLSessionAttrs function.
For more information on session-related functions, see "EGL library j2eeLib" in the EGL Language Reference.