Chapter 3b: JSP Basics 2 Sessions - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

Chapter 3b: JSP Basics 2 Sessions

Description:

Add Shopping Cart to Catalog. Reference: Example 2 (page 90) Add a BUY hyperlink to estore.jsp ... Increment and Return to Home Links. Add 1 hyperlink in shopcart.jsp ... – PowerPoint PPT presentation

Number of Views:51
Avg rating:3.0/5.0
Slides: 13
Provided by: conest
Category:

less

Transcript and Presenter's Notes

Title: Chapter 3b: JSP Basics 2 Sessions


1
Chapter 3b JSP Basics 2 Sessions JSPs
Reference Beginning JSP
2
Objectives
  • To learn
  • Decoding incoming URL parameters
  • Sessions and JSPs
  • Scoping of implicit objects in JSP
  • Shopping Cart Example
  • Adding a Shopping Cart
  • Rendering order information
  • Using a session to track line items

3
Simple Web Storefront
  • Shopping Cart Example

4
Add Shopping Cart to Catalog
  • Reference Example 2 (page 90)
  • Add a BUY hyperlink to estore.jsp
  • lta href"lt request.getContextPath()
    /example2/shopcart.jsp?actionbuysku"
    curItem.getSku() gt"gt
  • ltbgtBUYlt/bgt lt/agt
  • Hyperlinks URL
  • Calls shopcart.jsp
  • Two Parameters
  • action buy (from estore.jsp), inc (from
    shopcart.jsp)
  • sku items sku (of product selected)

5
Decoding Incoming Parameters
  • Decoding incoming URL parameters in shopcart.jsp
  • lt
  • int quan 1 // defaults to 1 if not
    specified in URL
  • String action request.getParameter("action")
  • if (action.equals("inc"))
  • String oldQuan request.getParameter("quan")
  • quan Integer.parseInt(oldQuan)
  • quan
  • // else - actionbuy
  • gt
  • Check for null parameters
  • lt
  • String sku request.getParameter("sku")
  • Product item null
  • if (sku ! null)
  • item EShop.getItem(sku)
  • gt

6
Increment and Return to Home Links
  • Add 1 hyperlink in shopcart.jsp
  • lta href"lt request.getContextPath() EXAMPLE
    CART_PAGE "?actionincquan" quan "sku"
    sku gt"gt
  • ltbgtAdd 1lt/bgtlt/agt
  • Alternative Use request.getRequestURL() if
    calling the same JSP page
  • Return hyperlink to return to estore.jsp
  • lta href"lt request.getContextPath() EXAMPLE
    SHOP_PAGE gt"gt
  • Return to Shoppinglt/agt

7
Overcoming Cart Limitations
  • Reference Example 3 (page 100)
  • To remember what has been ordered
  • Add a Java class named LineItem
  • Properties quantity, sku, description, price
  • In shopcart.jsp
  • Check if list of line items already exists in
    session object
  • ArrayList items (ArrayList) session.getAttribute
    ("lineitems")
  • Create and store ArrayList of LineItems objects
    collection to session object
  • if (items null) // add first item Buy
    link from estore.jsp
  • items new ArrayList()
  • items.add(new LineItem(1,sku,prod.getName(),
  • prod.getPrice() ))
  • session.setAttribute("lineitems", items)

8
Clear Cart and Increment
  • Clear Cart hyperlink in shopcart.jsp
  • lta href"lt request.getContextPath() EXAMPLE
    CART_PAGE "?actionclear" gt"gt
  • Clear the cartlt/agt
  • Executing Clear Cart action
  • else if (action.equals("clear"))
  • items.clear()
  • Add 1 (increment) action
  • for (int i0 iltitems.size() i)
  • LineItem curItem (LineItem)
    items.get(i)
  • if (curItem.getSku().equals(sku))
  • itemFound true
  • curItem.setQuantity(curItem.getQuant
    ity() 1)
  • break
  • // of if
  • //of for

9
Calculate and Display Total Price
  • Calculate Total Price
  • lt
  • for (int i0 ilt items.size() i)
  • LineItem curItem (LineItem) items.get(i)
  • int quan curItem.getQuantity()
  • long price curItem.getPrice()
  • long extended quan price
  • total extended
  • gt
  • Rendering total order price
  • lttd align"right"gtlt dispPrice(String.valueOf(tot
    al)) gtlt/tdgt

10
Scoping of Implicit Objects in JSP
  • Reference page 112
  • pageContext
  • Accessible within the current page
  • request
  • Accessible from all JSP pages that service the
    same request
  • session
  • Accessible from all the session-aware JSP pages
    that are accessed within the same session.
  • application
  • Accessible within all the JSP pages within the
    application. This is the global scope.

11
Application Scope Example
  • Reference examplex (page 113)
  • In showglob.jsp
  • lt
  • ArrayList cats (ArrayList)
    application.getAttribute("cats")
  • for (int i0 ilt cats.size() i)
  • Category curCat (Category)
    cats.get(i)
  • gt
  • lt curCat.getName() gt
  • ltbr/gt
  • lt
  • gt
  • Note You must run estore.jsp first
  • public void jspInit()
  • getServletContext().setAttribute("cats",
    EShop.getCats())

12
Next Steps
  • Try It Out sections of Textbook (Chapter 3)
  • Example 2, 3 and X
  • Learn to use the NetBeans 4.1 Debugger
  • Exercises 1 and 2 (Chapter 3)
Write a Comment
User Comments (0)
About PowerShow.com