<%@ page contentType="text/html; charset=GBK" %>
<%@ page import="Java.util.*" %>
<html><head><title>Cluster App Test</title></head>
<body>
Server Info:
<%
out.println(request.getLocalAddr() + " : " + request.getLocalPort()+"<br>");%>
<%
out.println("<br> ID " + session.getId()+"<br>");
// 如果有新的 Session 属性设置
String dataName = request.getParameter("dataName");
if (dataName != null && dataName.length() > 0) {
String dataValue = request.getParameter("dataValue");
session.setAttribute(dataName, dataValue);
}
out.print("<b>Session 列表</b>");
Enumeration e = session.getAttributeNames();
while (e.hasMoreElements()) {
String name = (String)e.nextElement();
String value = session.getAttribute(name).toString();
out.println( name + " = " + value+"<br>");
System.out.println( name + " = " + value);
}
%>
<form action="index.jsp" method="POST">
名称:<input type=text size=20>
<br>
值:<input type=text size=20>
<br>
<input type=submit>
</form>
</body>
</html>
然后在test 新建WEB-INF目录,WEB-INF下新建web.xml,内容如下
<web-app xmlns="" xmlns:xsi="" xsi:schemaLocation="" version="2.4">
<display-name>TomcatDemo</display-name>
<distributable/>
</web-app>
注意:在你的应用的web.xml加入 <distributable/>即可
ok,讲test复制到tomcat1,tomcat2的webapps下,重启apache,tomcat1,tomcat2,
输入网址
新建一个 名称为 xiaoluo ,值为 cdut 的session,提交查询,新开一个ie窗口,再提交查询,如图,可以看到,两个tomcat 是负载均衡,并且session同步的
需要注意的是,配置集群时设置jvmRoute时,要把原来的Engine注释掉,去掉上面那一行的注释,也就是修改成
<!-- You should set jvmRoute to support load-balancing via JK/JK2 ie : -->
<Engine defaultHost="localhost" debug="0" jvmRoute="tomcat1">