Session state in Websphere Commerce is saved in the Business Context, which is tied to the users ActivityToken.
Session state is serialized to the database, and will be available if the users session goes to another server in the cluster.
You can add your own session state by registering a new context element in BusinessContext.xml in the WC\xml\config\BusinessContext.xml, like so:
<BusinessContext ctxId="MyContext"
factoryClassname="com.ibm.commerce.context.factory.SimpleBusinessContextFactory" >
<parameter name="spiClassname" value="com.myorg.commerce.context.contentimpl.MyContextImpl" />
Then you need to tell which kinds of sessions your Context will be present in
<!-- web site store front configuration -->
<InitialBusinessContextSet ctxSetId="Store" >
...
<InitialBusinessContext ctxId="MyContext" createOrder="0" />
The context will be created along with all other contexts, and will be serialized to either the CTXDATA database table (for known users) and in a browser cookie for anonymous users.
Your context class should look something like this:
An interface class com.myorg.commerce.context.mycontextimpl.MyContext
public abstract interface MyContext extends Context
{
public static final String CONTEXT_NAME = "com.myorg.commerce.context.mycontextimpl.MyContext";
public abstract String getSomeValue();
public abstract void setSomeValue(String v);
}
And an implementation
public class MyContextImpl extends AbstractContextImpl
implements MyContext
{
}
After setting a new value, use "this.setDirty(true)" to flag the changes for persistance.
You must also override getContextAttributes to return the values of your context that needs to be serialized, and the setContextAttributes to re-establish the values.
The point is, that the context does more than simply store values. You put invariants in the context, that should hold true for all aspects of the users interaction with the site. The best example is the EntitlementContext, which holds which contract(s) you are buying under, which can be rather complicated to calculate.
Anyway, to access your context from a command, you'd use
this.getCommandContext().getContext(MyContext.CONTEXT_NAME);
And from a jsp
if (request.getAttribute("myContext") == null) {
request.setAttribute("myContext", ((CommandContext) request.getAttribute("CommandContext")).getContext(MyContext.CONTEXT_NAME));
}
after which you can use it as
${myContext.someValue}