EBIZ 5535 Lecture 3 - PowerPoint PPT Presentation

1 / 29
About This Presentation
Title:

EBIZ 5535 Lecture 3

Description:

that the browser returns unchanged when visiting the same Web site or domain later ... Then enclose the code for the dynamic parts in special tags, most of which start ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 30
Provided by: jeffreyv
Category:
Tags: ebiz | enclose | lecture | lousy

less

Transcript and Presenter's Notes

Title: EBIZ 5535 Lecture 3


1
EBIZ 5535Lecture 3
  • Handling Cookies

2
Outline
  • Overview of Cookies
  • The Servlet Cookie API
  • Reading Incoming Cookies
  • Creating Cookies
  • Placing Cookies in the Response Header
  • Long-Lived Cookies
  • Example
  • JSP

3
Overview of Cookies
  • Cookies are small bits of textual information
    that
  • a server sends to a browser
  • that the browser returns unchanged when visiting
    the same Web site or domain later
  • By having the server read information it sent the
    client previously, the site can provide visitors
    with a number of conveniences

4
Conveniences from Cookies
  • Identifying a user during an e-commerce session
  • Avoiding username and password
  • Customizing a site
  • Focusing advertising

5
The Servlet Cookie API
  • To send cookies to the client, a servlet would
  • Create one or more cookies with the appropriate
    names and values via
  • new Cookie(name, value)
  • Set any desired optional attributes via
    cookie.setXxx
  • Add the cookies to the response headers via
    response.addCookie(cookie)

6
Reading Incoming Cookies
  • Call request.getCookies(), which returns an array
    of Cookie objects
  • Loop down this array until you find the one whose
    name (getName) matches
  • Then call getValue on that Cookie to see the
    value associated with that name

7
Creating Cookies
  • A cookie is created by calling the Cookie
    constructor, which takes two strings
  • The cookie name
  • The cookie value
  • Neither the name nor the value should contain
    whitespace or any of
  • ( ) , " / ? _at_

8
Reading and Specifying Cookie Attributes
  • Before adding the cookie to the outgoing headers,
    you can look up or set attributes of the cookie.
    Here's a summary
  • getName/setName
  • getValue/setValue
  • getComment/setComment
  • getDomain/setDomain
  • getPath/setPath
  • getSecure/setSecure
  • getVersion/setVersion

9
Placing Cookies in the Response Headers
  • The cookie is added to the Set-Cookie response
    header by means of the addCookie method of
    HttpServletResponse
  • Cookie userCookie
  • new Cookie("user", "uid1234")
  • response.addCookie(userCookie)

10
Reading Cookies from the Client
  • Call getCookies on the HttpServletRequest
  • public static String
  • getCookieValue(Cookie cookies,
  • String cookieName,
  • String defaultValue) for(int i0
    iltcookies.length i)
  • Cookie cookie cookiesi
  • if (cookieName.equals(cookie.getName()))
    return(cookie.getValue()) return(defaultValue)

11
Long-Lived Cookies
  • Cookies that persist between browser sessions
  • import javax.servlet.http.
  • public class LongLivedCookie extends Cookie
    public static final int
  • SECONDS_PER_YEAR 606024365
  • public LongLivedCookie(String name,
  • String value)
  • super(name, value)
  • setMaxAge(SECONDS_PER_YEAR)

12
Example A Customized Search Engine
  • SearchEnginesFrontEnd.java
  • CustomizedSearchEngine.java

13
Session Tracking
  • HTTP is a stateless protocol
  • This makes many applications problematic
  • Example shopping carts
  • Typical solutions
  • Cookies
  • URL Rewriting
  • Hidden Form Fields
  • Servlets provide a nice solution
  • HttpSession API

14
Session Tracking
  • Looking up the HttpSession object associated with
    the current request
  • HttpSession session request.getSession(true)

15
Looking up Information Associated with a Session
  • Session objects are automatically associated with
    requesters
  • They have a built in data structure that allows
    you to keep any number of key-value pairs with
    the session object

16
Example of Looking up Session info
  • HttpSession session request.getSession(true)
  • ShoppingCart previousItems
  • (ShoppingCart)session.getAttribute("previousItems"
    )
  • if (previousItems ! null) doSomethingWith(previ
    ousItems)
  • else previousItems new ShoppingCart(...)
  • doSomethingElseWith(previousItems)

17
Associating Information with a Session
  • HttpSession session request.getSession(true)
  • session.setAttribute("referringPage",
  • request.getHeader("Referer"))
  • ShoppingCart previousItems (ShoppingCart)session
    .getAttribute("previousItems")
  • if (previousItems null)
  • previousItems new ShoppingCart(...)
  • String itemID request.getParameter("itemID")
  • previousItems.addEntry(Catalog.getEntry(itemID))
  • session.setAttribute("previousItems",
  • previousItems)

18
Example
  • ShowSession.java

19
JavaServer Pages
  • Lets you separate the dynamic part of your pages
    from the static HTML
  • write the regular HTML
  • Then enclose the code for the dynamic parts in
    special tags, most of which start with "lt" and
    end with "gt
  • Thanks for ordering ltIgtlt request.getParameter("t
    itle") gtlt/Igt

20
JSP Files
  • .jsp extension
  • Store with HTML files
  • Jsps are translated to servlets
  • Aside from regular HTML, there are three types of
    JSP constructs
  • scripting elements
  • directives
  • actions

21
JSP Scripting Elements
  • JSP scripting elements let you insert Java code
    into the servlet that will be generated from the
    current JSP page. There are three forms
  • Expressions of the form lt expression gt that
    are evaluated and inserted into the output
  • Scriptlets of the form lt code gt that are
    inserted into the servlet's service method, and
  • Declarations of the form lt! code gt that are
    inserted into the body of the servlet class,
    outside of any existing methods.

22
JSP Expressions
  • A JSP expression is used to insert Java values
    directly into the output.
  • lt Java Expression gt
  • The Java expression is evaluated, converted to a
    string, and inserted in the page.
  • This evaluation is performed at run-time (when
    the page is requested), and thus has full access
    to information about the request.

23
Example of an Expression
  • Current time
  • lt new java.util.Date() gt
  • Predefined variables (to simplify expressions)
  • request, the HttpServletRequest
  • response, the HttpServletResponse
  • session, the HttpSession associated with the
    request (if any) and
  • out, the PrintWriter (a buffered version of type
    JspWriter) used to send output to the client

24
JSP Scriptlets
  • If you want to do something more complex than
    insert a simple expression, use a JSP scriptlet
  • lt Java Code gt
  • Have access to the same automatically defined
    variables
  • lt String queryData request.getQueryString()
    out.println("Attached GET data " queryData)
    gt

25
Can Intermix HTML and Scriptlets
  • lt if (Math.random() lt 0.5) gt
  • Have a ltBgtnicelt/Bgt day!
  • lt else gt
  • Have a ltBgtlousylt/Bgt day! lt gt

26
  • if (Math.random() lt 0.5)
  • out.println("Have a ltBgtnicelt/Bgt day!")
  • else
  • out.println("Have a ltBgtlousylt/Bgt day!")

27
JSP Declarations
  • A JSP declaration lets you define methods or
    fields that get inserted into the main body of
    the servlet class
  • outside of the service method processing the
    request
  • lt! Java Code gt

28
Example
  • lt! private int accessCount 0 gt
  • Accesses to page since server reboot lt
    accessCount gt

29
JSP Directives
  • A JSP directive affects the overall structure of
    the servlet class
  • lt_at_ directive attribute"value" gt
  • Or
  • lt_at_ directive attribute1"value1"
  • attribute2"value2"
  • ...
  • attributeN"valueN" gt
Write a Comment
User Comments (0)
About PowerShow.com