Saving Client State - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

Saving Client State

Description:

... and shopping cart, the Writer, etc. ... Add to Cart /a /td /tr ' Using ... adds to the shopping cart. String bookId = request.getParameter('Buy' ... – PowerPoint PPT presentation

Number of Views:52
Avg rating:3.0/5.0
Slides: 30
Provided by: russell5
Category:
Tags: client | gocart | saving | state

less

Transcript and Presenter's Notes

Title: Saving Client State


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
(No Transcript)
5
(No Transcript)
6
(No Transcript)
7
Handling All Browsers
  • Session Tracking uses cookies by default to
    associate session id with user
  • If browser doesnt 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("lthtmlgt" ...) ...
// Get the catalog and send it, nicely
formatted BookDetails books
database.getBooksSortedByTitle()
... for(int i0 i lt numBooks i)
... //Print out
info on each book in its own two rows
out.println("lttrgt" ...
"lta href\""
response.encodeUrl("/servlet/bookdetails?bookId"

bookId) "\"gt ltstronggt"
booksi.getTitle() "
lt/stronggtlt/agtlt/tdgt" ...
"lta href\""
response.encodeUrl("/servlet/catalog?Buy"
bookId) "\"gt Add to
Cart lt/agtlt/tdgtlt/trgt"

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 users 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().get
ServletContext().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/h
tml") // now get the writer and write
the data of the response PrintWriter out
response.getWriter() out.println("lthtmlgt"
"ltheadgtlttitlegt Book
Catalog lt/titlegtlt/headgt" ...) ...
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
values0 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("lthtmlgt ltheadgt"
"lttitlegtYour Shopping Cartlt/titlegt" ...)
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
values0 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.setContentTy
pe("text/html") PrintWriter out
response.getWriter() //Print out the
response out.println("lthtmlgt ltheadgt"
"lttitlegtYour Shopping
Cartlt/titlegt" ...)
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(i0 i lt
cookies.length i) Cookie
thisCookie cookiei 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("lthtmlgt ltheadgt"
"lttitlegtYour Shopping Cartlt/titlegt" ...)
18
(No Transcript)
19
(No Transcript)
20
HTTP 1.0 Tokens
  • Tokens dont 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
(No Transcript)
23
(No Transcript)
24
(No Transcript)
25
(No Transcript)
26
(No Transcript)
27
(No Transcript)
28
(No Transcript)
29
ltHTMLgt ltHEADgt ltTITLEgtElizalt/TITLEgt lt/HEADgt ltBODYgt
ltAPPLET NAME"Eliza" CODE"ElizaApplet" WIDTH400
HEIGHT60gt ltPARAM NAME"server" VALUE"http//192.
168.0.1728080/servlet/eliza"gt ltPARAM
NAME"method"VALUE"GET"gtlt/APPLETgt lt/BODYgt lt/HTMLgt
Write a Comment
User Comments (0)
About PowerShow.com