15
Chapter 9 Session Tracking

Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an

Embed Size (px)

Citation preview

Page 1: Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an

Chapter 9

Session Tracking

Page 2: Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an

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

Page 3: Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an

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

Page 4: Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an

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)

Page 5: Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an

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

Page 6: Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an

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.

Page 7: Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an

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.

Page 8: Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an

ServletLogin.java

Page 9: Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an

ServletLogin.java display error message when user enter wrong username or password

Page 10: Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an

ServletAdminFunctions.java

Page 11: Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an

Servlet Admin function will show the functions which admin can use.

This Servlet have to check User Login or Not

Page 12: Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an

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.

Page 13: Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an

Template Interface

Page 14: Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an

ViewCart interface

Page 15: Chapter 9 Session Tracking. Session Tracking Basics Accessing the session object associated with the current request: Call request.getSession to get an

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%)