Title: Application State
1Application State
- Cookies, Session Management, and Top N Queries
October 3, 2001
2Administration
- Preliminary Design Documents
- We were very impressed!
- If we found glaring errors, you would have
received an email from us. - Egret
- If you believe you cannot log into egret, reread
the directions carefully, and if you still have
trouble, email TAs with your group number. - Common problems omitting cucs/ in your user
name, not using map network drive, and not
using the correct username/password we gave you
(it is NOT your CSUGLab netid/password!!)
3Final Design Documents
- Due October 10
- Information online under class notes about what
to include - Try to be complete, but concise (you dont need
100 pages ?).
4In the beginning
there was
5Next came
But what can we do with it?
6The Internet
We can offer services! Sell, buy and trade
things! Post information! Chat! Surf!
You are wise
Do not forget CS433
7The Internet
- The onset of the Web has brought about new
client-server issues - Reliability servers must be online 24-7
- Distributed networking servers access many
systems - Scalability servers must be able to handle
millions of requests - Stateless and State Maintained Interaction
8Stateless and State Maintenance
- Stateless
- No information is retained from one request to
another - Pure web servers are stateless
- State
- Some memory is stored between requests
- This memory can be saved on either the client or
the server
9Stateless Protocol
- Advantages
- Easy to use dont need anything
- Great for static-information applications
- Requires no extra memory space
- Disadvantages
- No record of previous requests means
- No shopping baskets
- No user logins
- No custom or dynamic content
- Security is more difficult to implement
10Application State
- Server-side state
- Information is stored in a database, or in the
application layers local memory - Client-side state
- Information is stored on the clients computer in
the form of a cookie - Hidden state
- Information is hidden within dynamically created
web pages
11Application State
So many kinds of state how will I choose?
12Server-Side State
- Many types of Server side state
- 1. Store information in a database
- Data will be safe in the database
- BUT requires a database access to query or
update the information - 2. Use application layers local memory
- Can map the users IP address to some state
- BUT this information is volatile and takes up
lots of server main memory
5 million IPs 20 MB
13Server-Side State
- Should use Server-side state maintenance for
information that needs to persist - Old customer orders
- Click trails of a users movement through a
site - Permanent choices a user makes
14Client-side State Cookies
- Storing text on the client which will be passed
to the application with every HTTP request. - Can be disabled by the client.
- Are wrongfully perceived as "dangerous", and
therefore will scare away potential site visitors
if asked to enable cookies1 - Are a collection of (Name, Value) pairs
1http//www.webdevelopersjournal.com/columns/state
ful.html
15Client State Cookies
- Advantages
- Easy to use in Java Servlets / JSP
- Provide a simple way to persist non-essential
data on the client even when the browser has
closed - Disadvantages
- Limit of 4 kilobytes of information
- Users can (and often will) disable them
- Should use cookies to store interactive state
- The current users login information
- The current shopping basket
- Any non-permanent choices the user has made
-
16Creating A Cookie
- Cookie myCookie
- new Cookie(username", jeffd")
- response.addCookie(userCookie)
- You can create a cookie at any time
17Accessing A Cookie
- Cookie cookies request.getCookies()
- String theUser
- for(int i0 iltcookies.length i)
- Cookie cookie cookiesi
- if(cookie.getName().equals(username))
theUser cookie.getValue() -
- // at this point theUser jeffd
- Cookies need to be accessed BEFORE you set your
response header - response.setContentType("text/html")
- PrintWriter out response.getWriter()
18Cookie Features
- Cookies can have
- A duration (expire right away or persist even
after the browser has closed) - Filters for which domains/directory paths the
cookie is sent to - See the Java Servlet API and Servlet Tutorials
for more information
19Hidden State
- Often users will disable cookies
- You can hide data in two places
- Hidden fields within a form
- Using the path information
- Requires no storage of information because the
state information is passed inside of each web
page
20Hidden State Hidden Fields
- Declare hidden fields within a form
- ltinput typehidden nameuser valuejeffd/gt
- Users will not see this information (unless they
view the HTML source) - If used prolifically, its a killer for
performance since EVERY page must be contained
within a form.
21Hidden State Path Information
- Path information is stored in the URL request
- http//server.com/index.htm?userjeffd
- Can separate fields with an character
- index.htm?userjeffdpreferencepepsi
- There are mechanisms to parse this field in Java.
Check out the javax.servlet.http.HttpUtils
parserQueryString() method.
22Multiple state methods
- Typically all methods of state maintenance are
used - User logs in and this information is stored in a
cookie - User issues a query which is stored in the path
information - User places an item in a shopping basket cookie
- User purchases items and credit-card information
is stored/retrieved from a database - User leaves a click-stream which is kept in a log
on the web server (which can later be analyzed)
23Case Study Top N Queries
- A Top N Query requests the first n records in
some table. - Used when there are many search results
- Want to break the search result into managable
batches, say to display 20 search result records
at a time (display NEXT, PREVIOUS 20) - You need to know the starting/ending point in
your table.
24Top N Queries
Select all the odd things Jeff wants in batches
of 2 items
Batch 1
Batch 2
Batch 3
Batch 4
25Placeholder Technique
- Store the first and last result tuples and
perform the same select query - Iterate through the results to return the next or
previous batch of results
26Placeholder Technique
- For each page you then know
- Showing Records 3-4
- Previous Link /display.jsp?lower3
- Next Link /display.jsp?upper4
- If the user clicks on the Next Link do
- Iterate to upper value 1 (here that is 5)
- Display the next 2 results
- Prepare new lower/upper values (5,6)
27Placeholder Technique
- You perform the SAME select query each time you
need a new batch of results - Advantages
- Easy implementation
- Disadvantages
- You must fetch many unneeded records (i.e. the
ones you iterate through)
28Query Constraint Technique
- Instead of issuing the SAME query, push the
constraint into the query so the result only
contains records that have not yet been displayed - You can use sorting to do this effectively
- Select P.Product
- From ProductTable P
- Where (P.Product item AND P.Id gt
lowest_value) - OR (P.Product gt item)
- Order By P.Product, P.Id
29Query Constraint Technique
- SELECT P.Product
- FROM ProductTable P
- WHERE (P.Product item AND P.Id gt
lowest_value) - OR (P.Product gt item)
- ORDER BY P.Product, P.Id
- What it means
- The first part of the WHERE clause is for when
there are many products with the same name, and
we only want to see the new ones. - The second part of the WHERE clause is for items
that are larger alphabetically.
30Query Constraint Technique
ID
Product
13
Baseball Hat
B1
27
Basketball Tickets
37
Basketball Tickets
B2
14
Laptop Computer
To move from batch B1 to batch B2 you would issue
SELECT P.Product FROM ProductTable P WHERE
(P.Product Basketball Tickets AND P.ID gt
27) OR (P.Product gt Basketball
Tickets) ORDER BY P.Product, P.ID
31Query Constraint Technique
- What information needs to be stored?
- Previous
- Title of FIRST record in the previous set
- Primary_Key of FIRST record in the previous set
- Next
- Title of FIRST record in the next set
- Primary_Key of FIRST record in next set
- This information can be encoded in the path
information
32Query Constraint Technique
- Advantages
- No need to iterate through results so you get a
large speedup - Disadvantages
- More difficult to implement
33Any questions?