Presentation is loading. Please wait.

Presentation is loading. Please wait.

Saving Client State Session Tracking: Maintain state about series of requests from same client over time Using Cookies: Clients hold small amount of their.

Similar presentations


Presentation on theme: "Saving Client State Session Tracking: Maintain state about series of requests from same client over time Using Cookies: Clients hold small amount of their."— Presentation transcript:

1 Saving Client State Session Tracking: Maintain state about series of requests from same client over time Using Cookies: Clients hold small amount of their state information. Servlets use information in cookie

2 Session Tracking Bookstore uses to keep track of books ordered Obtain session object for a user ( HttpSession ) (boolean for creation) Store or get data from object Invalidate session Session properties (session identifier) Key-value pairs for application data

3 Steps Obtain Session for a user Store or get data from session object Invalidate the session (manual or automatic) Bookstore uses session to keep track of books a user orders Shared by all servlets in application

4

5

6

7 Handling All Browsers Session Tracking uses cookies by default to associate session id with user If browser doesn’t support cookies, must use URL rewriting (not supported by servletrunner) Session Id included in links: session id sent as part of URL

8 public class CatalogServlet extends HttpServlet { public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Get the user's session and shopping cart, the Writer, etc.... // then write the data of the response out.println(" " +...);... // Get the catalog and send it, nicely formatted BookDetails[] books = database.getBooksSortedByTitle();... for(int i=0; i < numBooks; i++) {... //Print out info on each book in its own two rows out.println(" " +... "<a href=\"" + response.encodeUrl("/servlet/bookdetails?bookId=" + bookId) + "\"> " + books[i].getTitle() + " " +... "<a href=\"" + response.encodeUrl("/servlet/catalog?Buy=" + bookId) + "\"> Add to Cart " + }

9 Using Cookies Key-value pair Way for server to store information on client Server appends to HTTP response headers Client appends to HTTP request headers Cookies are single-valued

10 Procedure To send Cookie –Instantiate Cookie Object –Set attributes –send the cookie Get information from Cookie –Retrieve all cookies from the user’s request –Find cookie with name interested in –Get values from cookies

11 Cookie Drawbacks Can only be strings take up client disk space browsers limit their number and size

12 public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { BookDBServlet database = (BookDBServlet) getServletConfig().getServletContext().getServlet("bookdb"); // Check for pending adds to the shopping cart String bookId = request.getParameter("Buy"); //If the user wants to add a book, remember it by adding a cookie if (bookId != null) { Cookie getBook = new Cookie("Buy", bookId);... } // set content-type header before accessing the Writer response.setContentType("text/html"); // now get the writer and write the data of the response PrintWriter out = response.getWriter(); out.println(" " + " Book Catalog " +...);... }

13 public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... //If the user wants to add a book, remember it by adding a cookie if (values != null) { bookId = values[0]; Cookie getBook = new Cookie("Buy", bookId); getBook.setComment("User wants to buy this book " + "from the bookstore."); }... }

14 public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... /* Handle any pending deletes from the shopping cart */ String bookId = request.getParameter("Remove");... if (bookId != null) { // Find the cookie that pertains to the book to remove... // Delete the cookie by setting its maximum age to zero thisCookie.setMaxAge(0);... } // also set content type header before accessing the Writer response.setContentType("text/html"); PrintWriter out = response.getWriter(); //Print out the response out.println(" " + " Your Shopping Cart " +...);

15 public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... //If the user wants to add a book, remember it by adding a cookie if (values != null) { bookId = values[0]; Cookie getBook = new Cookie("Buy", bookId); getBook.setComment("User has indicated a desire " + "to buy this book from the bookstore."); response.addCookie(getBook); }... }

16 public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... /* Handle any pending deletes from the shopping cart */ String bookId = request.getParameter("Remove");... if (bookId != null) { // Find the cookie that pertains to the book to remove Cookie[] cookies = request.getCookies();... // Delete the book's cookie by setting its maximum age to zero thisCookie.setMaxAge(0); } // also set content type header before accessing the Writer response.setContentType("text/html"); PrintWriter out = response.getWriter(); //Print out the response out.println(" " + " Your Shopping Cart " +...);

17 public void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {... /* Handle any pending deletes from the shopping cart */ String bookId = request.getParameter("Remove");... if (bookId != null) { // Find the cookie that pertains to that book Cookie[] cookies = request.getCookies(); for(i=0; i < cookies.length; i++) { Cookie thisCookie = cookie[i]; if (thisCookie.getName().equals("Buy") && thisCookie.getValue().equals(bookId)) { // Delete the cookie by setting its maximum age to zero thisCookie.setMaxAge(0); } // also set content type header before accessing the Writer response.setContentType("text/html"); PrintWriter out = response.getWriter(); //Print out the response out.println(" " + " Your Shopping Cart " +...);

18

19

20 HTTP 1.0 Tokens Tokens don’t contain special characters reserved by RFC2068 Alphanumeric OK URLEncoder: Converts string to MIME format x-www-form-urlencoded Converts spaces to “+” Other characters to 3 character hex number “%xy”

21 Applets and Servlets Applet programming problem not servlet Let applet use http to communicate to servlet servlet can communicate with applet in text, binary, or html

22

23

24

25

26

27

28

29 Eliza


Download ppt "Saving Client State Session Tracking: Maintain state about series of requests from same client over time Using Cookies: Clients hold small amount of their."

Similar presentations


Ads by Google