session is getting reset in IBM Websphere Commerce

2k views Asked by At

I am setting a session in the jsp using scriplet in IBM WCS and setting a value here but when reloading the page the session value is getting lost .

here is how I am setting session attribute

<%
session.setAttribute("testMap", testValue);
%>

However on my local toolkit Its works fine ,but when it is deployed to server having this issue

Please suggest any solution regarding this

Thanks Ankit

2

There are 2 answers

0
Soraz On

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}

0
Michael Rasmussen On

The short answer is don't do this. WebSphere commerce is typically deployed in a distributed environment, and you might be seeing the effect of this when your code gets deployed. It is a lot of work for the application to persist the session across WebSphere nodes. Instead use a cookie, or create a database table. What are you trying to store in that map that has to be in session.