2

我正在使用 IBM WCS 中的 scriplet 在 jsp 中设置会话并在此处设置一个值,但是在重新加载页面时,会话值会丢失。

这是我设置会话属性的方式

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

但是在我的本地工具包上它工作正常,但是当它部署到有这个问题的服务器时

请就此提出任何解决方案

感谢 Ankit

4

2 回答 2

2

Websphere Commerce 中的会话状态保存在与用户 ActivityToken 相关联的业务上下文中。

会话状态被序列化到数据库中,并且如果用户会话转到集群中的另一台服务器则可用。

您可以通过在 WC\xml\config\BusinessContext.xml 中的 BusinessContext.xml 中注册一个新的上下文元素来添加自己的会话状态,如下所示:

 <BusinessContext ctxId="MyContext"
               factoryClassname="com.ibm.commerce.context.factory.SimpleBusinessContextFactory" >
<parameter name="spiClassname" value="com.myorg.commerce.context.contentimpl.MyContextImpl" />

然后你需要告诉你的 Context 将出现在哪些类型的会话中

<!-- web site store front configuration -->
<InitialBusinessContextSet ctxSetId="Store" >
    ...
  <InitialBusinessContext ctxId="MyContext" createOrder="0" />

上下文将与所有其他上下文一起创建,并将序列化到 CTXDATA 数据库表(对于已知用户)和匿名用户的浏览器 cookie。

您的上下文类应如下所示:

一个接口类 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);
}

一个实现 public class MyContextImpl extends AbstractContextImpl implements MyContext { }

设置新值后,使用“this.setDirty(true)”标记持久性更改。

您还必须重写 getContextAttributes 以返回需要序列化的上下文的值,并重写 setContextAttributes 以重新建立这些值。

关键是,上下文不仅仅是存储值。您将不变量放在上下文中,这应该适用于用户与站点交互的所有方面。最好的例子是 EntitlementContext,它保存了您购买的合同,计算起来可能相当复杂。

无论如何,要从命令访问您的上下文,您可以使用

this.getCommandContext().getContext(MyContext.CONTEXT_NAME);

并从一个jsp

if (request.getAttribute("myContext") == null) {
    request.setAttribute("myContext", ((CommandContext) request.getAttribute("CommandContext")).getContext(MyContext.CONTEXT_NAME));
}

之后您可以将其用作 ${myContext.someValue}

于 2015-05-06T20:51:17.647 回答
0

简短的回答是不要这样做。WebSphere commerce 通常部署在分布式环境中,当您的代码被部署时,您可能会看到这种效果。应用程序要跨 WebSphere 节点保持会话需要做很多工作。而是使用 cookie,或创建数据库表。您要在必须处于会话状态的地图中存储什么。

于 2015-05-06T14:47:33.610 回答