Title: Saving Client State
1Saving 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
2Session 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
3Steps
- 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)
7Handling 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"
9Using 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
10Procedure
- 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
11Cookie 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)
20HTTP 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
21Applets 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)
29ltHTMLgt 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