The Servlet and JSP Session Interface A Sample JSP Session Application

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

Tom cat uses t he st andard session int erface described in t he Java Servlet Specificat ion. This int erface can be used bot h by servlet s and by JSP pages. Wit hin a servlet , you gain access t o t he session by im port ing t he javax.servlet.http.HttpSession class and invoking t he getSession m et hod of your HttpRequest obj ect : import javax.servlet.http.; HttpSession session = request.getSession ; I n JSP pages, session support is enabled by default , so it s as t hough t hose st at em ent s have already been issued by t he t im e t he page begins execut ing. That is, t he session is available im plicit ly t hrough a session variable t hat s already been set up for you. The com plet e session int erface is defined in t he HttpSession sect ion of t he Java Servlet Specificat ion see Appendix C . Som e represent at ive m et hods of session obj ect s are list ed below: isNew Ret urns t rue or false t o indicat e whet her or not t he session has j ust begun wit h t he current request . getAttribute String attrName Session cont ent s consist of at t ribut es, which are obj ect s t hat are bound t o nam es. To access a session at t ribut e, specify it s nam e. The getAttribute m et hod ret urns t he Object bound t o t he given nam e, or null if t here is no obj ect w it h t hat nam e. setAttribute String attrName, Object obj Adds t he obj ect t o t he session and binds it t o t he given nam e. removeAttribute String attrName Rem oves t he at t ribut e wit h t he given nam e from t he session. invalidate I nvalidat es t he session and any dat a associat ed wit h it . The next request from t he client will begin a new session.

19.4.5 A Sample JSP Session Application

The following exam ple shows a JSP page, sess_t rack.j sp, t hat m aint ains a session request count er and a log of t he request t im es. To illust rat e t he session- relat ed operat ions m ore explicit ly, t his page consist s prim arily of em bedded Java code t hat uses t he HttpSession session int erface direct ly: -- sess_track.jsp - session request countingtimestamping demonstration - - page import=java.util. get session variables, initializing them if not present int count; Object obj = session.getAttribute count; if obj == null count = 0; else count = Integer.parseInt obj.toString ; ArrayList timestamp = ArrayList session.getAttribute timestamp; if timestamp == null timestamp = new ArrayList ; increment counter, add current timestamp to timestamp array count = count + 1; timestamp.add new Date ; if count 10 save updated values in session object { session.setAttribute count, String.valueOf count; session.setAttribute timestamp, timestamp; } else restart session after 10 requests { session.removeAttribute count; session.removeAttribute timestamp; } html head titleJSP Session Trackertitle head body bgcolor=white pThis session has been active for = count requests.p pThe requests occurred at these times:p ul for int i = 0; i timestamp.size ; i++ out.println li + timestamp.get i + li; ul body html I nvoke sess_t rack.j sp a few t im es from your browser t o see how t he display changes. The session.setAttribute m et hod used in sess_t rack.j sp places inform at ion int o t he session so t hat it can be found by lat er invocat ions of t he script . But session at t ribut es also can be shared wit h ot her script s. To see t his, m ake a copy of sess_t rack.j sp and invoke t he copy from your browser. Youll see t hat it accesses t he sam e session inform at ion as sess_t rack.j sp. Som e of t he session relat ed operat ions shown in sess_t rack.j sp can be done using t ags from JSTL, which provides a sessionScope variable for get t ing at t he im plicit JSP session obj ect : -- sess_track2.jsp - session request countingtimestamping demonstration -- page import=java.util. taglib uri=http:java.sun.comjstlcore prefix=c c:if test={empty sessionScope.count} c:set var=count scope=session value=0 c:if c:set var=count scope=session value={sessionScope.count+1} ArrayList timestamp = ArrayList session.getAttribute timestamp; if timestamp == null timestamp = new ArrayList ; add current timestamp to timestamp array, store result in session timestamp.add new Date ; session.setAttribute timestamp, timestamp; html head titleJSP Session Tracker 2title head body bgcolor=white pThis session has been active for c:out value={sessionScope.count} requests.p pThe requests occurred at these times:p ul c:forEach var=t items={sessionScope.timestamp} lic:out value={t} li c:forEach ul -- has session limit of 10 requests been reached? -- c:if test={sessionScope.count ge 10} c:remove var=count scope=session c:remove var=timestamp scope=session c:if body html

19.4.6 Telling Tomcat to Save Session Records in MySQL