HomePage » Tomcat » TomcatSession


Tomcat Sessions

Very often, you will need to store information on server-side session. Here is an example of session on tomcat, how to configure persistent session, and do session replication over a tomcat cluster.

Create a simple session app
First, create a simple application where you can input and retrieve session variables. I'll be using an application called TomcatSession. So I create this directory structure:
TOMCAT_HOME
|- webapps
|--- TomcatSession
|----- index.html
|----- WEB-INF
|------- classes
|------- web.xml
|--------- SessionExample.java
|--------- SessionExample.class


index.html just contains a link to launch the servlet. The web.xml defined how servlet are mapped to URL, and lastly, a java servlet to input and retrieve session variables.

<html>
<body>
<a href="/TomcatSession/SessionExample">Start session example app</a>
</body>
</html>


web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">


    <description>Session Example</description>
    <display-name>Session Example</display-name>

    <servlet>
        <servlet-name>SessionExample</servlet-name>
        <servlet-class>SessionExample</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>SessionExample</servlet-name>
        <url-pattern>/SessionExample</url-pattern>
    </servlet-mapping>

</web-app>


Compile SessionExample.java with this command line if you are not familiar with java classpath:
%JAVA_HOME%\bin\javac -cp .;w:\apache-tomcat-6.0.14\lib\servlet-api.jar SessionExample.java


If you play with the app, you will be able to input session variables and retrieve them by going to the same page.

Session Persistence
If you try restarting tomcat, you will realize the session variables are still there. That's because since tomcat5.5, sessions are made persistent by default. Sessions are written to disk on restart. To turn that off, edit TOMCAT_HOME/conf/context.xml and uncomment the following block -

context.xml
    <Manager pathname="" />


Notice if you do not specify a Manager, tomcat will start the default Manager automatically. By default, tomcat stores sessions in a file at this location TOMCAT_HOME\work\Catalina\localhost\TomcatSession\SESSIONS.ser. More information on SessionManager can be found at http://tomcat.apache.org/tomcat-4.1-doc/config/manager.html. From the link, you will be able to define other attributes on Session Manager. For example, the following allows you to specify where tomcat sessions should be stored on restart.

context.xml
<Manager pathname="/tmp/tomcat/" />


Tomcat cluster and Session Replication
To enable tomcat clustering, do the following

Step 1. Enable tomcat cluster in server.xml
Edit server.xml such that SimpleTcpCluster is enabled, and a jvmRoute is added.
server.xml
<!-- uncomment the followings -->
    <Engine name="Standalone" defaultHost="localhost" jvmRoute="jvm1">  
    <Cluster className="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>
<!-- get rid of the following -->
    <Engine name="Catalina" defaultHost="localhost">


Of course you will need to assign a different jvmRoute to different tomcat instances. Other than that, if you are running two or more tomcats on the same host, make sure the ports 8005, 8080, 8009 are changed on different instances so they won't fight for the same ports.

Step2: Modify your webapp such that tomcat knows sessions need to be replicated
Simply add a distributable tag in web.xml anywhere between the <web-app> block
<web-app>
	<distributable/>
</web-app>	


Step3: Fire up tomcats and test the same TomcatSession app again.
If you get it running correctly, you will see session appearing on the other tomcat instance right after you enter it in the current one. You should also notice the session id on different instances of tomcat are different:

On tomcat1
  Session Info
  Session ID: D0F607C86A3A4F22958E92A01F7E10E1.jvm1


On tomcat2
  Session Info
  Session ID: D0F607C86A3A4F22958E92A01F7E10E1.jvm2


Multicast issue

For security reasons, some network may not allow multucast traffic. This traffic is an essential communication protocol for memory-to-memory replication. If you suspect multicast is banned, run this tool to confirm.

On node1
> java -jar multicast_test.jar NODE1

On node2
> java -jar multicast_test.jar NODE2


If you receive messages from each other, multicast works.

That's it, you're done! That's how easy Java/Tomcat is!



Reference doc:
http://www.javaworld.com.tw/jute/post/view?bid=9&id=194224&sty=1&tpg=1&age=-1
http://tomcat.apache.org/tomcat-6.0-doc/cluster-howto.html
Comments [Hide comments/form]
Valid XHTML 1.0 Transitional :: Valid CSS :: Powered by WikkaWiki