Teaching Instructor: Shong
Class Hours: - minutes
l Technical Objectives of this chapter
N Learn how to manipulate objects in the session
N Use the associated properties and methods of the session
l The Focus of this chapter
N HttpSession of Understanding
N Use HttpSession for session tracking
l difficulties in this chapter
N Use HttpSession for session tracking
l work Tasks in this chapter
N Upgrade E-book Electronic Bookstore to realize the function of shopping cart
u add a book to a shopping cart
u View Shopping Cart Merchandise
l The Whole chapter teaching idea
N This chapter explains four questions in turn: a review E-book The electronic bookstore has realized the function, continues to realize E-book e-Bookstore Shopping cart, display shopping cart Map data and summary collection classes and interfaces in
n review e-book E-Bookstore has implemented features: review in the 3
N continue to achieve E-book the shopping cart of the electronic bookstore: In contrast to the actual shopping situation, in order to facilitate the pick-up of goods, use shopping carts and bags to load goods, in the program we use Map as a loading tool.
Nsummarizing collection classes and interfaces:Map,Listin the previous study has been contacted, andSetNo, so at the time of explanation, toSetfocused, but the main description of the use ofSetTraversalMapin the data, for the otherSetfeatures are not described.
Review : [5 minutes ]
in the Web Why do you use sessions in your project?
How do I access data in a session?
How do I create and end a session?
Preview Check : [5 minutes ]
Steps to implement a shopping system?
How to manage the quantity of each item in a shopping cart?
List , Set , Map Similarities and differences?
Course Knowledge Point explanation:
Review E-book The Electronic bookstore has implemented the function:[5 minutes ]
E-book e-Bookstore has implemented features
v Show list of all books
v View book details
continue to achieve E-book e-Bookstore Shopping cart [ minutes ]
Design analysis: What to accomplish in this chapter:
The design of the object in the shopping cart
Use Servlet implement add books to cart
Use JSP implement a list of books to view your shopping cart
Shopping Cart:
|--Books
| |-ID
|-- book title
|- | version
|-- price
|--Number of books
public class Cartitembean implements Serializable { Private Bookbean book; / / Current Books private int quantity; / / quantity in the shopping cart public Cartitembean (Bookbean booktoadd, int number) { book = Booktoadd; quantity = number; } public void setquantity (int number) { quantity = number; } public int getquantity () { return quantity; } . .. // the other Setter , Getter Method } |
The realization of the shopping cart
instructors should describe the whole business process, with special hints HashMap Is not the only way to do it, but it is also possible to use other collection classes.
Review: Map
What is Map ? What is the feature of Map?
Key-value pairs
Map How do I create an object?
New HashMap ()
How to Map To add data in?
Put (" key ", " value ") method
how to from Map To fetch data from?
Get (" key ");
Implement add books to Cart
v Get session Object
v To Remove a bookbean object from the session
v < Font face= "Times New Roman" >hashmap object map
... public void DoPost (HttpServletRequest request, HttpServletResponse response) throws Servletexception, IOException { HttpSession session = Request.getsession (false); Bookbean book = (Bookbean) session.getattribute ("Booktoadd"); Map cart = new HashMap (); cart.put (BOOK.GETISBN (), New Cartitembean (book, 1)); session.setattribute ("cart", cart); } ... |
Instructor Notes above key codes ( Red part ) .
Determine if the book exists in the shopping cart
v exists: quantity plus one
v Does not exist: Add entry to Map
HttpSession session = Request.getsession (false); Map cart = (map) session.getattribute ("cart"); if (cart = = null) { cart = new HashMap (); session.setattribute ("cart", cart); } Bookbean book = (Bookbean) session.getattribute ("Booktoadd"); Cartitembean cartitem = (Cartitembean) cart.get (BOOK.GETISBN ()); if (cartitem!= null) { cartitem.setquantity (cartitem.getquantity () + 1); } else{ Cart.put (BOOK.GETISBN (), New Cartitembean (book, 1)); } |
instructors should explain key codes ( Red part )
Show Shopping Cart Map data in [ minutes ]
View Shopping Cart List
from Session out of Map
from Map out of Cartitembean Data
Steering viewcart.jsp
<%@ page contenttype= "TEXT/HTML;CHARSET=GBK"%> Map cart = (map) session.getattribute ("cart"); Set cartitems = Cart.keyset (); object[] ISBN = Cartitems.toarray (); Bookbean Book; int i = 0; while (I < isbn.length) { Cartitem = (Cartitembean) cart.get ((String) isbn[i]); book = Cartitem.getbook (); / / display book data on the page , such as:book.gettitle (); i++; } |
instructor to explain key code ( Red part )
Set interface inheritance relationships and common methods
Field programming
What is the implementation step for adding a product to the shopping cart?
Instructor allows one to two trainees to perform field programming
Common errors:
1 : 404 error. Path error.
2 : The page does not display: May be the name of the wrong call.
can you use map size () key value value
Map does not have an integer index and cannot be exploited size () to cycle
Common errors
Indicate what is wrong with the red section below
<% Map cart = (map) session.getattribute ("cart"); Set cartitems = Cart.keyset (); object[] ISBN = Cartitems.toarray (); int i = 0; while (i < isbn.length) { Cartitembean Cartitem = (Cartitembean) cart.get (isbn[i)); bookbean book = cartitem.getbook (); } %> |
Get () method requires a String , so there is a need for forced type conversions.
not properly formatted ( slide )
Solution: <%= new DecimalFormat ("0.00"). Format (price) %>
summarizing collection classes and interfaces [ minutes ]
colloection features of the interface
The instructor should explain the above inheritance tree in detail and explain List and Set the difference.
Map the characteristics
The instructor should explain the above inheritance tree in detail, emphasizing Map is to store data in key-value pairs, with no order between the data.
Summary of this chapter [ ten minutes ]
How to understand Session role in the shopping system?
how to from Session value and save value in?
What are the common collection classes? What's the difference between them?
Assessment points
How to create a session
How to end a session
How to save and get data in a session
Extension section:
temporarily no
Student Problem Summary:
temporarily no
Homework:
&NBSP; < Span ' Times New Roman '; Mso-hansi-font-family: ' Times Roman ' "" "Exercise 1 : 92 the page to 93 page multiple-choice
&NBSP; < Span ' Times New Roman '; Mso-hansi-font-family: ' Times Roman ' "" "Exercise 2 : 93 the page 1,2,3,4
Exercise 3 : Preview the fifth chapter, try to do after the class choice question