25 Caching HTTP Sessions for WebLogic

The following example demonstrates how to use Coherence*Web to cache session information for Web application instances that are deployed across WebLogic application servers. In particular, this example creates a Web application and deploys it to two application server instances. The application is a simple counter that stores the current count as a session attribute. Coherence*Web automatically serializes and replicates the attribute across both server instances. Lastly, a browser is used to access each application instance to demonstrate that the same session attribute is used among the instances.

25.1 Requirements

To complete this example the following software must be installed:

  • Oracle Coherence 3.4

  • WebLogic 10.X (This example uses 10.3. WebLogic 8.X and 9.X are also supported.)

25.2 Install Coherence*Web on WebLogic 10.X

The Coherence*Web module includes a plug-in installer that supports WebLogic 10.X. Use the instructions located at the following link to install Coherence*Web to WebLogic:

Installing Coherence*Web Session Management Module on BEA WebLogic 10.x

Step three of the install procedure is completed later in this example.

25.3 Configure WebLogic

This example requires two application server instances:

  1. Run the Oracle WebLogic Configuration Wizard (BEA_HOME\wlserver_10.3\common\bin\config.exe or config.sh). Use the wizard to create a new WebLogic domain. From the wizard, customize the domain and configure two managed servers: ServerA using port 8080; and ServerB using port 8081. If you would like to use the WebLogic Node Manager (recommended), configure a Machine to host the application server instances and assign application server instances to the Machine. For this example, the Machine name used is test and the domain name used is test_domain.

  2. Before exiting the wizard, click to select the Start Admin Server check box, and click Done. The configuration wizard automatically starts the administration server. To manually start the administration server, run BEA_HOME/user_projects/domains/test_domain/startWebLogic.cmd or startWebLogic.sh.

  3. From a browser, log in to the Oracle WebLogic Server Administration Console using the following URL:http://hostname:7001/console. The console starts, and the domain home page displays.

  4. From the Domain Structure menu, expand Environment and click Servers. The Summary of Servers page displays and should be similar to Figure 25-1:

    Figure 25-1 Summary of Servers Page

    Description of Figure 25-1 follows
    Description of "Figure 25-1 Summary of Servers Page"

  5. If you are using the Node Manager, start the manager from a command prompt using BEA_HOME\wlserver_10.3\server\bin\startNodeManager.cmd or startNodeManager.sh.

  6. From the Summary of Servers screen, click the Control tab and start both server instances. If you are not using the Node Manager, manually start the server instances from the command line. Change directories to BEA_HOME\user_projects\domains\test_domain\bin and issue the following commands:

    To start the two server instances on Windows:

    startManagedWeblogic.cmd ServerA http://localhost:7001
    startManagedWeblogic.cmd ServerB http://localhost:7001
    

    To start the two server instances on Linux/UNIX:

    ./startManagedWeblogic.sh ServerA http://localhost:7001
    ./startManagedWeblogic.sh ServerB http://localhost:7001
    

25.4 Create the Counter Web Application

The Counter Web application is a simple counter implemented as a JSP. The counter is stored as an HTTP session attribute and increments each time the page is accessed.

To create the Counter Web application:

  1. Create a standard Web application directory as follows:

    /
    /WEB-INF
    
  2. Copy the following code to a text file:

    <?xml version = '1.0' encoding = 'windows-1252'?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" 
     xmlns="http://java.sun.com/xml/ns/j2ee" version="2.5">
       <description>Empty web.xml file for Web Application</description>       
    </web-app>
    
  3. Save the file as web.xml to the /WEB-INF directory.

  4. Copy the following code to a text file:

    <h3>
          Counter :
          <%
             Integer counter = new Integer(1);
             HttpSession httpsession = request.getSession(true);
             if (httpsession.isNew()) {
                    httpsession.setAttribute("count", counter);
                    out.println(counter);
             } else {
                    int count = ((Integer) httpsession.getAttribute("count")).intValue();
                    httpsession.setAttribute("count", new Integer(++count));
                    out.println(count);
             }
          %>
          </h3>
    
  5. Save the file as counter.jsp in the root of the Web application directory. The Web application directory should appear as follows:

    /
    /counter.jsp
    /WEB-INF/web.xml
    
  6. ZIP or JAR the Web application directory and save the file as counter.war.

25.5 Modify the Counter Web Application to use Coherence*Web

All Web applications that want to take advantage of Coherence*Web must be modified to include the required Coherence*Web libraries and configuration files. Coherence includes an installer that automatically adds the necessary files to a Web application. To run the installer, follow the instructions located at:

General Instructions for Installing Coherence*Web Session Management Module

The installation is a 2-step process:

  • The inspect step—In the inspect step, a coherence-web.xml file is created for the Counter Web application.

  • The install step—In the install step, the Counter Web application's web.xml is modified based on the content of the coherence-web.xml. In addition, the application is modified to include the coherence.jar, coherence-web.jar, and tangosol.jar; and the session-cache-config.xml configuration file.

The coherence-web.xml file can be edited to change the behavior of the session cache. For example, the default session model used is the split model. The coherence-sessioncollection-class parameter can be modified to use a different session model (that is, monolithic or traditional).

Note:

Changes to the coherence-web.xml file must be made before the install step.

For more information on how a Web application is modified during installation, see How the Coherence*Web Installer Instruments a Java EE Application.

25.6 Deploy the Application

To deploy the application:

  1. From a browser, log in to the Oracle WebLogic Server Administration Console using the following URL:

    http://host:7001/console

    The console starts and the domain home page displays.

  2. From the Domain Structure menu, click deployments. The Summary of Deployments page displays.

  3. Click Install. The Install Application Assistant screen displays.

  4. Use the Install Application Assistant to deploy counter.war to both ServerA and ServerB. The Summary of Deployments page displays after the application is deployed. Figure 25-2 illustrates the deployments table with the counter Web application.

Figure 25-2 Deployments Window Showing the Deployed Application

Description of Figure 25-2 follows
Description of "Figure 25-2 Deployments Window Showing the Deployed Application"

25.7 Verify the Example

To verify the example:

  1. Open a browser and access the ServerA Counter instance using the following URL:

    http://host:8080/counter/counter.jsp

    The counter page displays and the counter is set to 1 as follows:

    Figure 25-3 Counter Page with Counter Set to 1

    Description of Figure 25-3 follows
    Description of "Figure 25-3 Counter Page with Counter Set to 1"

  2. From the same browser (or in a new browser tab), access the ServerB Counter instance using the following URL:

    http://host:8081/counter/counter.jsp

    The counter page displays and the counter is incremented to 2 based on the session data: If you refresh the page, the counter is incremented to 3. Refresh the instance on ServerA and the counter is at 4.

    Figure 25-4 Counter Page with Counter Set to 4

    Description of Figure 25-4 follows
    Description of "Figure 25-4 Counter Page with Counter Set to 4"

25.8 Summary

This example demonstrated how Coherence*Web is used to cache session data across multiple Web application instances deployed to multiple WebLogic server instances.