Database Reference
In-Depth Information
To access session variables, use the $_SESSION superglobal array after calling ses
sion_start() . For example, the session variable named count is available as $_SES
SION["count"] .
21.4. Using MySQL for Session-Backing Store with Tomcat
Problem
You want to use session storage for Java-based scripts.
Solution
Tomcat handles session management for you. By default, it uses temporary files for
backing store. To configure it to use MySQL instead, modify the appropriate Tomcat
configuration file to supply JDBC parameters.
Discussion
The Perl, Ruby, and PHP session mechanisms described earlier in this chapter require
applications to indicate explicitly that they want to use MySQL-based session storage.
For Perl and Ruby, a script must state that it wants to use the appropriate session module.
For PHP, the session manager is built into the language, but each application that uses
a MySQL storage module must register it.
For Java applications that run under Tomcat, a different framework applies. Tomcat
itself manages sessions, so to store session information in MySQL, reconfigure Tomcat,
not your applications. This relieves web-based Java programs of some of the messy
session-related details handled at the application level in other languages. For example,
the Tomcat server rather than your application handles session IDs. If cookies are en‐
abled, Tomcat uses them. Otherwise, it uses URL rewriting to encode the session ID in
the URL. Application developers need not care which method Tomcat uses because the
ID is available the same way for either method.
To illustrate the independence of applications from the session management method
used by Tomcat, this section shows simple JSP application scripts that use a session.
Then it shows how to reconfigure Tomcat to store session information in MySQL rather
than in the default session store—without requiring any changes at all to the application
scripts.
This section assumes that the mcb application has been installed into and unpacked
under the Tomcat webapps directory (see Recipe 18.3 ). For background on Tomcat itself,
read “JSP, JSTL, and Tomcat Primer” on the companion website (see the Preface ).
Search WWH ::




Custom Search