Tomcat Cluster Testing

A sample test file (sesstest.jsp):

<%@page language="java" %>
<html>
<body>
<h1><font color="red">Session serviced by machine1</font></h1>
<table align="center" border="1">
  <tr>
  <td>
     Session ID
  </td>
  <td>
     <%= session.getId() %></td>
  </td>
  <% session.setAttribute("abc","abc");%>
  </tr>
  <tr>
  <td>
    Created on
  </td>
  <td>
    <%= session.getCreationTime() %>
  </td>
  </tr>
</table>
</body>
</html>

Put the above file on each instance and change the machine name.

Another test file:

<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Date"%>

<%@page import="java.util.List"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<title>JSP Page</title>
<span style="color: blue; font-size: small;">Instance 1 </span>
<hr>
<span style="color: #cc0000; font-size: small;">
        Session Id : <%=request.getSession().getId()%> 
        Is it New Session : <%=request.getSession().isNew()%>
        Session Creation Date : <%=new Date(request.getSession().getCreationTime())%>
        Session Access Date : <%=new Date(request.getSession().getLastAccessedTime())%>
</span>
<b>Cart List </b>
<hr>
<ul>
        <%
                String bookName = request.getParameter("bookName");
                List<string> listOfBooks = (List<string>) request.getSession().getAttribute("Books");

                if (listOfBooks == null) {
                    listOfBooks = new ArrayList<string>();
                    request.getSession().setAttribute("Books", listOfBooks);
                }
                if (bookName != null) {
                    listOfBooks.add(bookName);
                    request.getSession().setAttribute("Books", listOfBooks);
                }
                for (String book : listOfBooks) {
                    out.println("<li>"+book + "</li>");
                }
        %>
</ul>
<hr>
        <form action="index.jsp" method="post">
            Book Name <input name="bookName" type="text">

            <input value="Add to Cart" type="submit">
        </form>
<hr>

How to test session replication?

  1. Ensure that the <Cluster>, <Manager> elements are uncommented, and make sure that the <Context> element is commented out on all instances.
  2. Start all instances
  3. Start Apache server with mod_jk or mod_proxy
  4. Open a browser to http://load-balancer-ip/example/jsp/sesstest.jsp
  5. Check that its working correctly, then shutdown the instance that had your session
  6. Retest using the browser and hopefully it should pickup the other instance but have the same session ID
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License