Server-side  Web Programming Lecture 9:  Shopping Carts and the Model-View-Control Architecture
Modeling the Business Process <ul><li>What  information  should a session deal with? </li></ul><ul><ul><li>What informatio...
Model-View-Control Architecture <ul><li>Model  = software classes that store/manipulate information gathered in session </...
Model Classes and Session <ul><li>Bad design: Store  all  session information as  separate attributes </li></ul><ul><ul><l...
Model Classes and Session <ul><li>Better design: Create  classes  for blocks of information </li></ul><ul><ul><li>Store  o...
Model Class Properties <ul><li>State variables for all information stored </li></ul><ul><li>set Variable  methods for each...
Example Customer Class
Creating Support Classes <ul><li>File    New File </li></ul><ul><li>Choose category  Java , type  class </li></ul>
Creating Support Classes <ul><li>As before, enter name </li></ul><ul><li>Tomcat requires support classes to be in a  packa...
Using Model Classes <ul><li>Must include package name at top of file package  packagename; </li></ul><ul><li>Other classes...
Using Model Classes <ul><li>In servlet: </li></ul><ul><li>Construct a new instance of the model object </li></ul><ul><li>U...
Using Model Classes <ul><li>In JSP: </li></ul><ul><li>Retrieve the object from the session attributes </li></ul><ul><ul><l...
Business Model Objects <ul><li>Key idea: </li></ul><ul><li>Methods in model objects should  implement business model </li>...
Business Model Example <ul><li>Order  class </li></ul><ul><ul><li>Methods to set/get quantity </li></ul></ul><ul><ul><li>M...
Order Class
Validation in Servlet
Getting Cost in JSP
Shopping Carts <ul><li>Usually  list  of items </li></ul><ul><ul><li>List =  Vector  or  ArrayList  type in Java </li></ul...
Example “Bookstore” Cart <ul><li>Cart : list of  Item  objects </li></ul><ul><li>Each customer has own  Cart  object store...
“ Bookstore” Cart Methods <ul><li>Usual methods: </li></ul><ul><li>void addItem(code)   </li></ul><ul><ul><li>add a new it...
“ Bookstore” Cart Methods
“ Bookstore” Item Methods <ul><li>Usual methods: </li></ul><ul><li>Item(code)   </li></ul><ul><ul><li>construct a new item...
“ Bookstore” Item Methods
Displaying Cart Contents <ul><li>Get cart from session </li></ul><ul><li>Inside of a table: </li></ul><ul><ul><li>Get numb...
Inside table Size of loop Get ith item object Get its fields Inside loop, create new table row showing those fields
Displaying Cart Contents
Displaying Cart Contents
Adding to the Cart in a Servlet <ul><li>Get current Cart object from session using  getAttribute </li></ul><ul><ul><li>If ...
Adding to the Cart in a Servlet <ul><li>Get data from request and pass to Cart </li></ul><ul><ul><li>Cart will construct a...
Adding to the Cart in a Servlet
Adding to the Cart in a Servlet <ul><li>Can use servlet to modify Cart in other ways </li></ul><ul><ul><li>Example: Remove...
Embedded Forms in Cart Pages <ul><li>Often embed buttons and other form elements into the rows of a Cart page </li></ul><u...
Embedded Forms in Cart Pages <ul><li>Must nest  entire form  inside a  <td>  element </li></ul><ul><ul><li>Action = servle...
Simple Removal Servlet
Hidden Form Elements <ul><li>Must submit  product code  for remove to work </li></ul><ul><li>Product code  not  displayed ...
Hidden Form Elements productCode read in from Cart in session and stored in hidden element inside removal form
Quantity Update Example
Quantity Update Example Pass productCode as hidden field so servlet knows which Item to change Also pass new quantity ente...
Passing Data using Links <ul><li>Many web sites use  html links  instead of forms </li></ul><ul><li>Question: How can  for...
Passing Data using Links <ul><li>Can append “form data” directly to URL in link </li></ul><ul><ul><li>Result similar to “g...
Passing Data using Links
Upcoming SlideShare
Loading in …5
×

Lecture9

492 views

Published on

Published in: Technology, Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
492
On SlideShare
0
From Embeds
0
Number of Embeds
6
Actions
Shares
0
Downloads
9
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide
  • Business logic: quy trinh nghiep vu
  • Lecture9

    1. 1. Server-side Web Programming Lecture 9: Shopping Carts and the Model-View-Control Architecture
    2. 2. Modeling the Business Process <ul><li>What information should a session deal with? </li></ul><ul><ul><li>What information must be gathered via forms? </li></ul></ul><ul><ul><ul><li>Items to purchase </li></ul></ul></ul><ul><ul><ul><li>Customer information, etc. </li></ul></ul></ul><ul><ul><li>What information must be displayed to the user in response pages? </li></ul></ul><ul><ul><ul><li>Shopping cart contents, etc. </li></ul></ul></ul><ul><ul><li>What information must be stored long term ? </li></ul></ul><ul><ul><ul><li>Throughout session </li></ul></ul></ul><ul><ul><ul><li>Longer (in databases, etc.) </li></ul></ul></ul><ul><li>Model of business process </li></ul><ul><ul><li>Defined by organization you develop site for </li></ul></ul>
    3. 3. Model-View-Control Architecture <ul><li>Model = software classes that store/manipulate information gathered in session </li></ul><ul><ul><li>Usually separate from servlets/JSPs </li></ul></ul><ul><ul><li>Servlets/JSPs interact with those classes </li></ul></ul><ul><ul><li>Often interact with databases </li></ul></ul>request Control servlet View JSP response View JSP View JSP Classes that model business process Store data Access and display data Database
    4. 4. Model Classes and Session <ul><li>Bad design: Store all session information as separate attributes </li></ul><ul><ul><li>May be dozens of attributes </li></ul></ul><ul><ul><li>Servlet/JSP responsible for manipulating each individually </li></ul></ul>All session data Session ID = fieh4K39Rdk … … … … Session data name email “ Fred” “ fred@aolrock”
    5. 5. Model Classes and Session <ul><li>Better design: Create classes for blocks of information </li></ul><ul><ul><li>Store objects as session attributes </li></ul></ul><ul><ul><li>Use class methods to store/access/manipulate data in object </li></ul></ul>All session data Session ID = fieh4K39Rdk … … … … Session data customerInfo cart Customer object Cart object
    6. 6. Model Class Properties <ul><li>State variables for all information stored </li></ul><ul><li>set Variable methods for each variable </li></ul><ul><ul><li>Takes value as parameter and stores it in state variable </li></ul></ul><ul><ul><li>Usually called by servlet to store data from parameters </li></ul></ul><ul><li>get Variable methods for each variable </li></ul><ul><ul><li>Returns current state variable </li></ul></ul><ul><ul><li>Usually called by JSP to display data stored earlier </li></ul></ul><ul><li>These model classes often created by other programmers </li></ul><ul><ul><li>Business logic specialists create model </li></ul></ul><ul><ul><li>Server programming specialists create servlets/JSPs </li></ul></ul>
    7. 7. Example Customer Class
    8. 8. Creating Support Classes <ul><li>File  New File </li></ul><ul><li>Choose category Java , type class </li></ul>
    9. 9. Creating Support Classes <ul><li>As before, enter name </li></ul><ul><li>Tomcat requires support classes to be in a package </li></ul><ul><ul><li>“ Library ” of related classes </li></ul></ul><ul><ul><li>Can define one at this point (added to drop down in future) </li></ul></ul>
    10. 10. Using Model Classes <ul><li>Must include package name at top of file package packagename; </li></ul><ul><li>Other classes that use class must import the package </li></ul><ul><ul><li>Servlets use import statement </li></ul></ul><ul><ul><li>JSPs use <@ page import tag </li></ul></ul>
    11. 11. Using Model Classes <ul><li>In servlet: </li></ul><ul><li>Construct a new instance of the model object </li></ul><ul><li>Use its set methods to store parameters from form </li></ul><ul><li>Store the object as a session attribute </li></ul>
    12. 12. Using Model Classes <ul><li>In JSP: </li></ul><ul><li>Retrieve the object from the session attributes </li></ul><ul><ul><li>Must cast back to its original type </li></ul></ul><ul><li>Use its get methods to retrieve data </li></ul><ul><li>Display the data on the page </li></ul>
    13. 13. Business Model Objects <ul><li>Key idea: </li></ul><ul><li>Methods in model objects should implement business model </li></ul><ul><ul><li>Methods to validate values </li></ul></ul><ul><ul><li>Methods to perform computations </li></ul></ul><ul><ul><li>Methods to store information in database </li></ul></ul><ul><li>Goal: </li></ul><ul><li>Separate web programming and business knowledge as much as possible </li></ul>
    14. 14. Business Model Example <ul><li>Order class </li></ul><ul><ul><li>Methods to set/get quantity </li></ul></ul><ul><ul><li>Method to get total cost of order </li></ul></ul><ul><ul><ul><li>Computed from quantity here instead of in JSP/servlet </li></ul></ul></ul><ul><ul><li>Method to check whether quantity valid in terms of business model </li></ul></ul><ul><ul><ul><li>Servlet makes sure quantity is a number </li></ul></ul></ul><ul><ul><ul><li>Business model class makes sure quantity is at least 1 </li></ul></ul></ul>
    15. 15. Order Class
    16. 16. Validation in Servlet
    17. 17. Getting Cost in JSP
    18. 18. Shopping Carts <ul><li>Usually list of items </li></ul><ul><ul><li>List = Vector or ArrayList type in Java </li></ul></ul><ul><ul><li>Has methods to add new element to end, get i th element, and remove i th element </li></ul></ul><ul><li>Each list element is business model object </li></ul><ul><ul><li>Has fields for all relevant data about item purchased </li></ul></ul><ul><ul><ul><li>Set and get methods for each </li></ul></ul></ul><ul><ul><ul><li>One field should be unique identifier (key field) </li></ul></ul></ul><ul><ul><ul><li>Some fields populated by database lookup </li></ul></ul></ul><ul><ul><li>May have way to set quantity purchased </li></ul></ul><ul><ul><ul><li>Not all models have quantity – course registration, for example </li></ul></ul></ul>
    19. 19. Example “Bookstore” Cart <ul><li>Cart : list of Item objects </li></ul><ul><li>Each customer has own Cart object stored in their session </li></ul><ul><li>Each Item has a unique code </li></ul><ul><ul><li>Given a code , should look up other fields in database </li></ul></ul><ul><ul><li>title and price </li></ul></ul><ul><ul><li>Not implemented yet! </li></ul></ul>Cart object Session ID: 98A6F401BC6393 Item object Code: 0001 Title: Murach's Java Servlets and JSP Price: $31.19 Quantity: 1 Item object Code: 0003 Title: HTML and XHTML Pocket Reference Price: $10.39 Quantity: 2
    20. 20. “ Bookstore” Cart Methods <ul><li>Usual methods: </li></ul><ul><li>void addItem(code) </li></ul><ul><ul><li>add a new item to the cart with the given ID </li></ul></ul><ul><li>Item lookup(code) </li></ul><ul><ul><li>Lookup and return the Item in the list with the given ID </li></ul></ul><ul><li>void removeItem(code) </li></ul><ul><ul><li>Lookup and delete the Item in the list with the given ID </li></ul></ul><ul><li>void getItem(int) </li></ul><ul><ul><li>Return the i th Item in the list </li></ul></ul><ul><li>void size() </li></ul><ul><ul><li>Return the number of Items in the list </li></ul></ul>Used together to loop through and show all Items on “shopping cart” page
    21. 21. “ Bookstore” Cart Methods
    22. 22. “ Bookstore” Item Methods <ul><li>Usual methods: </li></ul><ul><li>Item(code) </li></ul><ul><ul><li>construct a new item </li></ul></ul><ul><ul><li>Look up rest of fields in database based on code </li></ul></ul><ul><li>String get______() </li></ul><ul><ul><li>Return the value of the given field </li></ul></ul><ul><ul><li>For bookstore: code, title, price, cost </li></ul></ul><ul><li>void setQuantity(int) int getQuantity() </li></ul><ul><ul><li>Since can order multiple copies, need ways to change quantity </li></ul></ul>
    23. 23. “ Bookstore” Item Methods
    24. 24. Displaying Cart Contents <ul><li>Get cart from session </li></ul><ul><li>Inside of a table: </li></ul><ul><ul><li>Get number of items in cart </li></ul></ul><ul><ul><li>Use loop to get each item in sequence </li></ul></ul><ul><ul><ul><li>Number of items = length of loop </li></ul></ul></ul><ul><ul><ul><li>Get the i th Item object from the Cart </li></ul></ul></ul><ul><ul><li>Get desired fields of that item </li></ul></ul><ul><ul><li>Create new table row showing those fields </li></ul></ul>
    25. 25. Inside table Size of loop Get ith item object Get its fields Inside loop, create new table row showing those fields
    26. 26. Displaying Cart Contents
    27. 27. Displaying Cart Contents
    28. 28. Adding to the Cart in a Servlet <ul><li>Get current Cart object from session using getAttribute </li></ul><ul><ul><li>If null , no Cart exists yet </li></ul></ul><ul><ul><li>In that case, construct one </li></ul></ul>
    29. 29. Adding to the Cart in a Servlet <ul><li>Get data from request and pass to Cart </li></ul><ul><ul><li>Cart will construct and store a new Item </li></ul></ul><ul><ul><li>Will need to validate request first </li></ul></ul><ul><li>Will need to check whether item already in Cart </li></ul><ul><ul><li>Need to avoid duplicate entries in Cart </li></ul></ul><ul><ul><li>Business model defines how handled </li></ul></ul><ul><ul><ul><li>Error message </li></ul></ul></ul><ul><ul><ul><li>Change quantity, </li></ul></ul></ul><ul><ul><ul><li>Add to quantity, etc. </li></ul></ul></ul>
    30. 30. Adding to the Cart in a Servlet
    31. 31. Adding to the Cart in a Servlet <ul><li>Can use servlet to modify Cart in other ways </li></ul><ul><ul><li>Example: Remove from Cart if quantity = 0 </li></ul></ul><ul><li>Store modified Cart to session using setAttribute </li></ul>
    32. 32. Embedded Forms in Cart Pages <ul><li>Often embed buttons and other form elements into the rows of a Cart page </li></ul><ul><ul><li>Example: simple REMOVE button and quantity updates </li></ul></ul>
    33. 33. Embedded Forms in Cart Pages <ul><li>Must nest entire form inside a <td> element </li></ul><ul><ul><li>Action = servlet to handle desired change to the cart </li></ul></ul><ul><ul><li>Contains SUBMIT button to send request </li></ul></ul><ul><ul><li>Contains form element with data to submit with request </li></ul></ul>
    34. 34. Simple Removal Servlet
    35. 35. Hidden Form Elements <ul><li>Must submit product code for remove to work </li></ul><ul><li>Product code not displayed on page inside a form element </li></ul><ul><ul><li>Common for most ecommerce pages </li></ul></ul><ul><li>Can use hidden form element </li></ul><ul><li>Not shown by browser </li></ul><ul><li>Can store product code or other information that we need to send to server </li></ul><ul><li><input type=“ hidden ” name=“ parametername ” value=“<%= product code %>” </li></ul>
    36. 36. Hidden Form Elements productCode read in from Cart in session and stored in hidden element inside removal form
    37. 37. Quantity Update Example
    38. 38. Quantity Update Example Pass productCode as hidden field so servlet knows which Item to change Also pass new quantity entered by user
    39. 39. Passing Data using Links <ul><li>Many web sites use html links instead of forms </li></ul><ul><li>Question: How can form information (such as product code) be passed if no form is used? </li></ul>
    40. 40. Passing Data using Links <ul><li>Can append “form data” directly to URL in link </li></ul><ul><ul><li>Result similar to “get” method in form </li></ul></ul><ul><li>Syntax: <a href = “ url of servlet ? name = value & name = value… ”> </li></ul><ul><ul><li>Note: will need to use response.encodeURL to insure this works if cookies not enabled </li></ul></ul><ul><li><a href = “<%=response.encodeURL(‘ servleturl ? name = value & … ’)%>”> </li></ul>Submit request to the servlet The ‘?’ indicates form parameters Each passed as a name=value pair separated by ‘&’
    41. 41. Passing Data using Links

    ×