May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 11
Objectives
Objectives
•
Review Usability•
Web Application Characteristics•
Review Servlets•
Deployment•
Sessions, CookiesMay 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 22
Usability
Usability
•
Trunk TestHarder than you probably thought Your answers didn’t always agree Important because of search engines
•
AmazonLocal navigation
•
IMDbOnly one with breadcrumb but least necessary?
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 33
Usability
Usability
•
TimeSectioning
Local Navigation is down the page “below the fold”
•
Encyclopedia BritannicaTable of Contents
•
Lessons:Popular sites aren’t necessarily the most usable? Hmm…
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 44
Discussion of
Discussion of
“
“
Quality Attributes
Quality Attributes
”
”
•
More to usability than navigationHow easy to do the functionality
•
What are some of the differences between traditional applications and web applications?Leads to differences in quality attributes
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 55
Comparison of Applications
Comparison of Applications
Frequent (~days), inexpensive Infrequent (~monthly), expensive ReleasesReturning customers; later but better
Time to market Economics
Traditional languages and Scripting languages, HTML, Other languages Java, C, C++, etc. Languages Programmers, graphics designers, usability engineers, Network, DB Programmers Development Team Network, DB Technologies
Client, Server (& more) On clients Location Web Applications Traditional Attribute May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 66
Quality Attributes
Quality Attributes
Later but better is okay Time-to-market
Short maintenance cycle, frequent updates Maintainability
Thousands of requests per second, more? Scalability
24/7/365 Availability
Must work, or go to another site Reliability
Protect user data, information Security
Must be usable, or go to another site Usability
Web Applications Attribute
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 77
How Have Web Sites Changed to
How Have Web Sites Changed to
Meet Quality Demands?
Meet Quality Demands?
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 88
How Have Web Sites Changed to
How Have Web Sites Changed to
Meet Quality Demands?
Meet Quality Demands?
•
Additional tiersAdd security, robustness
•
Additional servers, databasesRunning in parallel Add availability
•
New technologiesBetter programming languages for writing, maintaining code
Separating presentation from business logic
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 99
Discussion
Discussion
•
Paper was written in 2002: Did anything seem dated?May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 1010
Servlets
Servlets
Review
Review
•
What application do we need to execute Servlets?•
What class do all Servlets extend?•
What methods do Servlets need to override to handle GET and POST requests?•
How do Servlets send an HTML document/response to the client?•
How do Servlets get data from the client?May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 1111
The Example Servlet Flow
The Example Servlet Flow
Server Web Application Server Client HTTP Request, data Response HTML Form HTML Document Web Browser subm it doGet doGet () () May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 1212
Lab
Lab
4: Modify
4: Modify
SurveyServlet
SurveyServlet
•
Currently: Inefficient implementation•
In initAutomatically called by server on start up Open file, read/initialize votes
•
In destroyAutomatically called by server Write file
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 1313
Web App Directory Structure
Web App Directory Structure
•
projectname/XHTML, CSS, and JSP files
•
projectname/WEB-INFOther resources, e.g., web.xml
•
projectname/WEB-INF/classesServlet and utility (data structures, etc) Why we put our Servlets in servlets package
•
projectname/WEB-INF/libJar files that application depends on
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 1414
web.xml
web.xml
File
File
•
Describes how to deploy the web application•
XML fileUsed for data
Marked up with elements
Same rules as XHTML: close most recently opened tag, attributes in quotes
•
DTD: Document Type DefinitionDefine elements that can be in a particular XML document
Includes specification of attributes, nesting
<tag attr=“value”> Content </tag>
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 1515
web.xml
web.xml
File
File
•
Top-level: <webapp>•
<servlet> element describes a servlet•
<servlet-mapping> elements maps URLs to ServletsMay want to have shorthands, aliases Restrict users’ direct access to servlets
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 1616
web.xml
web.xml
File
File
•
Subelements of <servlet>optional parameter containing a name-value pair that is passed to the servlet on initialization. Contains elements, <param-name> and <param-value>, which contain the name and value, respectively, to be passed to the servlet.
<init-param>
fully qualified class name of the servlet
<servlet-class>
canonical name of the deployed servlet
<servlet-name>
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 1717
Lab 4: Configure
Lab 4: Configure
SurveyServlet
SurveyServlet
•
Configure SurveyServlet to use a given file•
Add the following to web.xml file: <init-param><param-name>surveyfile</paramname> <param-value>survey.results</param-value> </init-param>
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 1818
Modify
Modify
init
init
Method
Method
•
Use ServletConfig’s getInitParameter method // calls HttpServlet methodfilename = getInitParameter(”surveyfile"); // open file …
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 1919
Deployment: WAR files
Deployment: WAR files
•
Web ArchivesAnalog to JAR files
•
Copy into webapps directory of web application serverServer will automatically extract files and run Procedure for Apache/Tomcat and Resin
•
Can export WAR files from EclipseMay 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 2020
Maintaining State
Maintaining State
•
If you have multiple pages, how can you save or accumulate data?Example scenario: buying a book
Login
Form WhichBook CreditCard Verify
Server Server Server Server
D1 D1 D1, D2 D1, D2 D1, D2, D3
D1, D2,
D3 D1, D2,D3, D4
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 2121
Maintaining State
Maintaining State
•
If you have multiple pages, how can you save or accumulate data?Hidden fields (type=hidden) Cookies
Sessions
Login
Form WhichBook CreditCard Verify
Server Server Server Server
D1 D1 D1, D2 D1, D2 D1, D2, D3
D1, D2,
D3 D1, D2,D3, D4
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 2222
Hidden Fields
Hidden Fields
•
Data is coming from client•
Users can see the hidden fieldsView HTML Source
•
Users can change the data
Useful in limited situations<input type=“hidden” name=“userid” value=“superfly”/>
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 2323
Cookies
Cookies
• A popular way to customize web pages is to use cookies
• Cookies are sent from the webapp to the client • Part of an HTTP header in the response to a client
Every HTTP transaction includes HTTP headers Not part of the HTML content
• Store information on the client’s computer • Client includes cookies in HTTP headers in
subsequent requests
Provides way to do behavior tracking
• Do you see any problems with cookies?
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 2424
Process with Cookies
Process with Cookies
Client Web App
Server Web Browser
• Cookies
Associated with server name Part of HTTP Headers
• Example: Amazon.com
Cookie stores your name, login information Example: Not Sara?
HTTP Request, Cookies Response: Cookies, HTML Document Cookies
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 2525
Cookies in Java
Cookies in Java
•
Cookies have a name and value•
Create a Cookie object using its constructorPart of javax.servlet.http.Cookie
•
Example: store a user’s preferred language on the clientApp only has to ask for this information once
String cookie_name = new String(“pref_language”); String cookie_value = new String(“English”);
Cookie new_cookie = new Cookie(cookie_name, cookie_value);
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 2626
Sending the Cookie to the Client
Sending the Cookie to the Client
• HTTP header is sent first
• Cookie(s) must be added to the response object before you start writing to the client
• Call addCookie() on HttpServletResponse object before you call the getWriter() method • Inside of doGet or doPost method:
Cookie c = new Cookie( “pref_language”, “English” ); c.setMaxAge(60*60*24*365); // max age of cookie response.addCookie(c);
. . .
output = response.getWriter();
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 2727
The
The
HttpServletResponse
HttpServletResponse
Object
Object
•void addCookie(Cookie)Add a Cookie to the header in the response to the client
The cookie will be stored on the client,
depending on the max-life and if the client allows cookies
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 2828
Cookies: Maximum Ages
Cookies: Maximum Ages
•
The maximum age of the cookie is how long the cookie can live on the client (in seconds)•
When a cookie reaches its maximum age, client deletes it•
-1 means persists until browser shuts down c.setMaxAge(60*60*24*365); // max age of cookieMay 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 2929
Retrieving Cookies
Retrieving Cookies
• Call getCookies() on HttpServletRequest object
Returns an array of Cookie objects
Represents all cookies that server previously sent to the client
• For example, inside of doPost
Cookie[] cookies = request.getCookies();
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 3030
Voiding Cookies
Voiding Cookies
•
May want to delete cookies when user logs outEspecially for sensitive information
// void cookie and send back to the user userid_cookie.setMaxAge(0);
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 3131
Session
Session
•
One user’s visit to an application•
Can be made up of many requests•
Server maintains session with particular clientCan maintain state within that session
•
Server has a timeoutIf no requests from client for specified period of time (the timeout), user’s session ends
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 3232
Session variables
Session variables
•HttpSession object can store data for session
Simpler for developer Reduce network traffic Known as session attributes
• Have names and values
• Store, access, and remove attributes:
Like a HashMap
void setAttribute(String name, Object value)
• Values no longer need to be strings
• Cookies and Parameters had to be strings Object getAttribute(String name)
void removeAttribute(String name)
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 3333
Example Session Variables
Example Session Variables
• User gives application data
• Application stores data in session variable session.setAttribute("username", username); • Application can use later in session, without user
having to give information every time String username =
session.getAttribute(“username”); • More examples:
Server computes information once, caches in session Shopping carts
name
value
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 3434
Getting a Session
Getting a Session
•
HttpServletRequest’sgetSession(boolean) method
Returns the current HttpSession object Boolean parameter specifies if a new session
should be created if one does not already exist
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 3535
Other Useful Session Methods
Other Useful Session Methods
•setMaxInactiveInterval(), getCreationTime(),
getLastAccessedTime()
If want shorter than server’s timeout
•invalidate()
Invalidates session, unbinds objects bound to it
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 3636
Lab 4: Add
Lab 4: Add
Session Variable
Session Variable
•
LoginServlet will add a session variable with name “authenticated”May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 3737
ServletContext
ServletContext
•
Share state among multiple clientsAllow multiple users to interact in, e.g., chat rooms, online meeting, reservation systems
•
Info about servlet’s environmentE.g., server’s name
•
log(): method to write to a log file•
Context attributesgetAttribute, setAttribute, removeAttribute
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 3838
Eclipse Development Hints
Eclipse Development Hints
•
Eclipse: allocated more memory•
Safe bet: restart server whenever change to a servlet or web.xml file•
Typical programmingWrite a few lines of code/make small changes Run, test
Repeat
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 3939
Project Discussion
Project Discussion
May 2, 2008
May 2, 2008 Sprenkle - CS297Sprenkle - CS297 4040