Establishing a session transaction for Web 2.0 channel

About this task

After the toolkit application is initialized, you need to establish session by using the CreateSession servlet class.

Procedure

  1. Define the Web 2.0 channel parameter in btt.xml:
    <kColl id="web2_xml">
      <field id="requestHandler"
        value="com.ibm.btt.channel.Web2RequestHandler" />
      <field id="presentationHandler"
        value="com.ibm.btt.channel.Web2PresentationHandler" />
      <field id="processor"
        value="com.ibm.btt.channel.BTTMessageProcessor" />
      <field id="cookies" value="false" />
      <field id="encoding" value="UTF-8" />
      <field id="runInSession" value="true" />
      <field id="requestContext" value="XmlChannelContext" />
      <field id="dataFormatter" value="XmlChannelFormatter" />
      <field id="replyContext" value="XmlReplyContext" />
      <field id="replyFormatter" value="XmlReplyFormatter" />
      <field id="sessionReplyContext" value="XmlSessionReplyContext" />
      <field id="sessionReplyFormatter" value="XmlSessionReplyFormatter" />
    </kColl>
    
    <kColl id="WEB2Client">
      <field id="startUpOp" value="CreateSessionOpStep" />
    </kColl>
  2. Define the Web 2.0 client startup operation in CreateSessionOpStep.xml:
    <CreateSessionOpStep.xml>
        <operation id="CreateSessionOpStep" context="myContext"
            implClass="com.ibm.btt.sample.opstep.CreateSessionOpStep">
        </operation>
    
        <context id="myContext" type="op">
            <refKColl refId="myCtxData" />
        </context>
    
        <kColl id="myCtxData">
            <field id="test" />
        </kColl>
    </CreateSessionOpStep.xml>
    Note: You can also define the context and data element as in the HTML channel sample.
  3. Implement Web 2.0 client startup Operation in <toolkit_root>/samples/BTTMultiChannleSample/SampleBusiness/com.ibm.btt.sample.opstep.CreateSessionOpStep.java:
    public class CreateSessionOpStep extends BTTServerOperation {
    @Override
    public void execute() throws Exception {
    Context root = ContextFactory.getRoot();
    if (root == null) {
    System.out.println("Creating root ctx");
    root = ContextFactory.createContext("branchServer", false);
    }
    System.out.println("Create session context");
    Context sessionCtx = ContextFactory.createContext("sessionCtx", false);
    sessionCtx.chainTo(root);
    Context ctx = getContext();
    ctx.chainTo(sessionCtx);
    }
    }
  4. Implement Web 2.0 client startup Servlet in <toolkit_root>/samples/BTTMultiChannleSample/BTTSampleWeb/com.ibm.btt.xml.servlet.CreateSession:
    KeyedCollection settings = ChannelInitializer.getSettings();
    String startupOp = (String) settings.tryGetValueAt(ChannelConstant.STARTUPOP);
    if (startupOp != null) {
        Operation oper = (BTTServerOperation) BTTServerOperation.readObject(startupOp);
        oper.execute();
        final Context context = oper.getContext();
        final Context sessionCtx = context.getParent();
        final HttpSession session = request.getSession();
        final SessionEntry se = new SessionEntry(session); 
        se.setSessionId(session.getId());
        se.setCurrentContext(sessionCtx);
        CSSessionHandler.addSession(se);
        response.setContentType("text/html");
        ServletOutputStream o=response.getOutputStream();
        String message="create sessionContext successful...";
     
    }
    Note: This class does the following:
    • Gets the startup Operation name from settings
    • Creates the startup operation
    • Executes startup operation to create rootContext and sessionContext
    • Initiates SessionEntry
    • Adds SessionEntry to CSSessionHandler
    • Updates the ChannelContext with the session