Problem Solution Discussion Using MySQL for Session BackingStore with Tomcat

? body html sess_t rack2.php is ident ical t o sess_t rack.php, wit h t wo except ions: • sess_t rack.php calls session_start t o open a session, but t hat is not st rict ly required, because it uses session_register , which im plicit ly opens t he session for you. sess_t rack2.php does not use session_register . I nst ead, it get s t he variable values direct ly from global session variable st orage. Wit h t hat approach, you m ust call session_start first t o open t he session explicit ly. • I f t he session lim it of 10 request s has not yet been reached, sess_t rack2.php explicit ly st ores t he count and timestamp session values back int o t he global session variable arrays by invoking set_session_val .

19.4 Using MySQL for Session BackingStore with Tomcat

19.4.1 Problem

You want t o use session st orage for Java-based script s.

19.4.2 Solution

Tom cat handles session m anagem ent for you. By default , it uses t em porary files for backing st ore, but you can configure it t o use MySQL inst ead by supplying t he appropriat e JDBC param et ers in Tom cat s server.xm l configurat ion file.

19.4.3 Discussion

The Perl and PHP session m echanism s described earlier in t his chapt er bot h require applicat ions t o indicat e explicit ly t hat t hey want t o use MySQL- based session st orage. For Perl, a script m ust st at e t hat it want s t o use t he appropriat e Apache: : Session m odule. For PHP 4, t he session m anager is built int o t he language, but each applicat ion t hat want s t o use t he MySQL st orage m odule m ust regist er it . For Java applicat ions t hat run under Tom cat , a different fram ework applies. Tom cat it self m anages sessions, and if you want t o st ore session inform at ion in MySQL, you do so by reconfiguring Tom cat , not your applicat ions. I n ot her words, web-based Java program s are relieved of som e of t he m essy session- relat ed det ails t hat m ust be handled at t he applicat ion level in ot her languages. For exam ple, session I Ds are handled by t he Tom cat server rat her t han at t he applicat ion level. Tom cat checks whet her cookies are enabled, and uses URL rewrit ing t o encode t he session I D in t he URL if cookies are unavailable. Applicat ion developers need not care which m et hod is used, because t he I D is available t he sam e way regardless of how it s t ransm it t ed. To illust rat e t he independence of applicat ions from t he session m anagem ent m et hod used by Tom cat , t his sect ion shows a sim ple JSP applicat ion t hat uses a session. Then it shows how t o reconfigure Tom cat t o st ore session inform at ion in MySQL rat her t han in t he default session st ore—wit hout requiring any changes at all t o t he applicat ion. First , t hough, it s necessary t o describe t he session int erface.

19.4.4 The Servlet and JSP Session Interface