Download presentation
Presentation is loading. Please wait.
Published byChastity Gardner Modified over 9 years ago
1
Chapter 9 Session Tracking
2
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
3
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
4
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)
5
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
6
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.
7
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.
8
ServletLogin.java
9
ServletLogin.java display error message when user enter wrong username or password
10
ServletAdminFunctions.java
11
Servlet Admin function will show the functions which admin can use. This Servlet have to check User Login or Not
12
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.
13
Template Interface
14
ViewCart interface
15
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%)
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.