[ Pobierz całość w formacie PDF ]
.Clients can,however, turn off the browser’s ability to accept cookies.If your sitemust track a client who has turned off cookies, then another method of sessiontracking (URL rewriting or hidden form fields) must be incorporated by hand,since the session tracking capabilities built into the servlet API are designedaround cookies.[ Add Comment ]The Cookie classThe servlet API (version 2.0 and up)provides the Cookie class.This class incorporates all the HTTP headerdetails and allows the setting of various cookie attributes.Using the cookie issimply a matter of adding it to the response object.The constructor takes acookie name as the first argument and a value as the second.Cookies are addedto the response object before you send any content.Cookie oreo = new Cookie("TIJava", "2000");res.addCookie(oreocookie);Cookies are recovered by calling thegetCookies( ) method of the HttpServletRequest object, whichreturns an array of cookie objects.Cookie[] cookies = req.getCookies();You can then call getValue( )for each cookie, to produce a String containing the cookie contents.Inthe above example, getValue("TIJava") will produce a Stringcontaining “2000.”[ Add Comment ]The Session classA session is one or more page requests bya client to a Web site during a defined period of time.If you buy groceriesonline, for example, you want a session to be confined to the period from whenyou first add an item to “my shopping cart” to the point where youcheck out.Each item you add to the shopping cart will result in a new HTTPconnection, which has no knowledge of previous connections or items in theshopping cart.To compensate for this lack of information, the mechanicssupplied by the cookie specification allow your servlet to perform sessiontracking.[ Add Comment ]A servlet Session object lives onthe server side of the communication channel; its goal is to capture useful dataabout this client as the client moves through and interacts with your Web site.This data may be pertinent for the present session, such as items in theshopping cart, or it may be data such as authentication information that wasentered when the client first entered your Web site, and which should not haveto be reentered during a particular set of transactions.[ Add Comment ]The Session class of the servletAPI uses the Cookie class to do its work.However, all the Sessionobject needs is some kind of unique identifier stored on the client and passedto the server.Web sites may also use the other types of session tracking butthese mechanisms will be more difficult to implement as they are notencapsulated into the servlet API (that is, you must write them by hand to dealwith the situation when the client has disabled cookies).[ Add Comment ]Here’s an example that implementssession tracking with the servlet API://: c15:servlets:SessionPeek.java// Using the HttpSession class.import java.io.*;import java.util.*;import javax.servlet.*;import javax.servlet.http.*;public class SessionPeek extends HttpServlet {public void service(HttpServletRequest req,HttpServletResponse res)throws ServletException, IOException {// Retrieve Session Object before any// output is sent to the client.HttpSession session = req.getSession();res.setContentType("text/html");PrintWriter out = res.getWriter();out.println("<HEAD><TITLE> SessionPeek ");out.println(" </TITLE></HEAD><BODY>");out.println("<h1> SessionPeek </h1>");// A simple hit counter for this session.Integer ival = (Integer)session.getAttribute("sesspeek.cntr");if(ival==null)ival = new Integer(1);elseival = new Integer(ival.intValue() + 1);session.setAttribute("sesspeek.cntr", ival);out.println("You have hit this page <b>"+ ival + "</b> times.<p>");out.println("<h2>");out.println("Saved Session Data </h2>");// Loop through all data in the session:Enumeration sesNames =session.getAttributeNames();while(sesNames.hasMoreElements()) {String name =sesNames.nextElement().toString();Object value = session.getAttribute(name);out.println(name + " = " + value + "<br>");}out.println("<h3> Session Statistics </h3>");out.println("Session ID: "+ session.getId() + "<br>");out.println("New Session: " + session.isNew()+ "<br>");out.println("Creation Time: "+ session.getCreationTime());out.println("<I>(" +new Date(session.getCreationTime())+ ")</I><br>");out.println("Last Accessed Time: " +session.getLastAccessedTime());out.println("<I>(" +new Date(session.getLastAccessedTime())+ ")</I><br>");out.println("Session Inactive Interval: "+ session.getMaxInactiveInterval());out.println("Session ID in Request: "+ req.getRequestedSessionId() + "<br>");out
[ Pobierz całość w formacie PDF ]