Upload
chastity-gardner
View
213
Download
0
Embed Size (px)
Citation preview
Chapter 9
Session Tracking
Session Tracking Basics Accessing the session object
associated with the current request: Call request.getSession to get an
HttpSession object It is a simple hash table for storing user-
specific data. Looking up information associated with
a session: Call getAttribute on the HttpSession
object cast the return value to the appropriate
type, and check whether the result is null
Session Tracking Basics
Storing information in a session: Use setAttribute with a key and a value.
Discarding session data Call removeAttribute to discard a
specific value. Call invalidate to discard an entire
session. Call logout to log the client out of the
Web server and invalidate all sessions associated with that user
The Session-Tracking API
Here is a summary of the methods available in the HttpSession class: public Object getAttribute(String name) public Enumeration getAttributeNames() public void setAttribute(String name,
Object value) public void removeAttribute(String name)
A Servlet That Shows Per-Client Access Counts
Listing 9.1: presents a simple servlet that shows basic information about the client's session
When the client connects, the servlet uses request.getSession either to retrieve the existing session or, if there is no session, to create a new one
A Servlet That Shows Per-Client Access Counts The servlet then looks for an attribute
called accessCount of type Integer. If it cannot find such an attribute, it uses 0
as the number of previous accesses. This value is then incremented and
associated with the session by setAttribute. Finally, the servlet prints a small HTML
table showing information about the session.
Practice Exercise
Write a servlet for user log to our system. If the user enter correct username and password, it will show the admin function. If the user enter incorrect username or password, it will show the error login page.
ServletLogin.java
ServletLogin.java display error message when user enter wrong username or password
ServletAdminFunctions.java
Servlet Admin function will show the functions which admin can use.
This Servlet have to check User Login or Not
Practice Exercise 2
Design a servlet page display 3 types of computer devices (Main board, CPU, Monitor).
Allow user enter number of device which they want to buy.
There are a button for user add device to their cart.
And there are a Link for user view their cart.
Template Interface
ViewCart interface
Homework exercise
Upgrade Practice Exercise 2 with new features:
1. Allow user remove computer devices to their cart.
2. Allow user update the quantity of devices when they view their cart
3. Calculate the total the customer must pay include VAT (Value added tax 10%)