Beruflich Dokumente
Kultur Dokumente
359
Handling Cookies
: 2011
Cookies
Agenda
Understanding the benefits and drawbacks of cookies Sending outgoing cookies Receiving incoming cookies Tracking repeat visitors Specifying cookie attributes Differentiating between session cookies and persistent cookies Simplifying cookie usage with utility classes Modifying cookie values Remembering user preferences g
Cookies
Idea
Servlet sends a simple name and value to client. Client returns same name and value when it connects to same site (or same domain, depending on cookie domain settings).
Usually browsers keep less than 20 cookies per site and less than 300 in general Cookie size < 4KB
10
Cookies
The browser receives and stores the response with the Cookie
Cookies
Cookies
Cookies
(cont)
String title; if (newbie) { Cookie returnVisitorCookie = new Cookie("repeatVisitor", "yes"); returnVisitorCookie.setMaxAge(60*60*24*365); returnVisitorCookie setMaxAge(60*60*24*365); response.addCookie(returnVisitorCookie); title = "Welcome Aboard"; } else { l title = "Welcome Back"; } response.setContentType("text/html"); ( / ) PrintWriter out = response.getWriter(); // (Output page with above title)
18
(cont)
first time
next times
Note: The server does not know who the visitor is. The server can only understand whether the visitor is NEW or NOT
19
getMaxAge/setMaxAge
Gets/sets the cookie expiration time (in seconds). If you fail to set this, cookie applies to current browsing session only. See L l S LongLivedCookie h l Li dC ki helper class given l t l i later.
getName
Gets the cookie name There is no setName method; you name. supply name to constructor. For incoming cookie array, you use getName to find the cookie of interest.
20
getSecure/setSecure
Gets/sets flag indicating whether cookie should apply only to SSL connections or to all connections. l t ti t ll ti
getValue/setValue
Gets/sets value associated with cookie. For new cookies, cookie cookies you supply value to constructor, not to setValue. For incoming cookie array, you use getName to find the cookie of interest then call getValue on the result. If you interest, result set the value of an incoming cookie, you still have to send it back out with response.addCookie.
21
24
25
26
27
28
29
30
(cont)
out.println(docType out println(docType + "<HTML>\n" + "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" + "<BODY BGCOLOR=\"#FDF5E6\">\n" + "<CENTER>\n" + "<H1>" + title + "</H1>\ " + titl "</H1>\n" "<H2>This is visit number " + count + " by this browser.</H2>\n"+ "</CENTER></BODY></HTML>"); / / / ) } }
32
(cont)
34
(cont)
public class Deletion extends HttpServlet { String description = "<BR><small>" + It shows all cookies in the request. <BR> " + "Subsequently it sets their age = 0 <BR> " + The deletion is evident by pressing reload." + "</small><BR><HR>"; "</small><BR><HR>" public void doGet(HttpServletRequest request, HttpServletResponse response) throws ... { PrintWriter out = response.getWriter(); out.println("<HTML><BODY> + description); Cookie[] cookies = request.getCookies(); if (cookies != null) {// showing all cookies out.println("<TABLE>"); t i tl ("<TABLE>") for(Cookie cookie: cookies) { out.println("<TR><TD>"); out.println(cookie.getName() + "</TD><TD>" </TD><TD> +cookie.getValue() + "</TD>"); } out.println("</TABLE>"); }
35
(cont)
// Deleting the cookies if (cookies != null) { for(Cookie cookie: cookies) { cookie.setMaxAge(0); cookie setMaxAge(0); response.addCookie(cookie); } } } // doGet } // servlet se et
36
Registration servlet
Creates cookies based on request parameters received Displays values if all parameters are present R di t t f Redirects to form (RegistrationForm servlet) if any (R i t ti F l t) parameter is missing
37
RegistrationForm Servlet
public class RegistrationForm extends HttpServlet { p public void doGet(HttpServletRequest request, ( p q q , HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response getWriter(); response.getWriter(); String actionURL = "coreservlets.RegistrationServlet"; String firstName = CookieUtilities.getCookieValue(request, "firstName", ""); String lastName = CookieUtilities.getCookieValue(request, CookieUtilities getCookieValue(request "lastName", ""); String emailAddress = CookieUtilities.getCookieValue(request, "emailAddress", "");
38
Registration Servlet
public class RegistrationServlet extends HttpServlet { p public void doGet(HttpServletRequest request, ( p q q , HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); boolean isMissingValue = false; String firstName = request.getParameter("firstName"); if (isMissing(firstName)) { firstName = "Missing first name"; isMissingValue = true; } String lastName = request.getParameter("lastName"); if (isMissing(lastName)) { lastName = "Missing last name"; isMissingValue = true; }
40
42
43
44
45
Summary
Basic functionality Let you
C ki involve name/value pairs sent from server to Cookies i l / l i tf t browser and automatically returned when the same page, site, or domain is visited later Track sessions (use higher-level session-tracking API) gg g y Permit users to avoid logging in at low-security sites Customize sites for different users Focus content or advertising
Setting cookies
Reading cookies g
46
Call Cookie constructor, set age, call response.addCookie Call request.getCookies, check for null, look through array for matching name, use associated value
Sources
Slides of Marty Hall, http://courses.coreservlets.com/
Various additions by the instructor
47