Top Banner
Week 6 • JSP’s and JavaBean Page Scope • JSP’s and JavaBean Request Scope • JSP’s and JavaBean Session Scope • JSP’s and JavaBean Application Scope • A Shopping cart application using JSP and JavaBeans • JSP and XML Much of this lecture is from a nice little book entitled “Pure JSP” by Goodwill
48

Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

Dec 20, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

Week 6

• JSP’s and JavaBean Page Scope

• JSP’s and JavaBean Request Scope

• JSP’s and JavaBean Session Scope

• JSP’s and JavaBean Application Scope

• A Shopping cart application using JSP and

JavaBeans

• JSP and XMLMuch of this lecture is from a nice littlebook entitled “Pure JSP” by Goodwill

Page 2: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

Page Scope

Beans with page scope are accessible only

within the page where they were created.

A bean with page-level scope is not

persistent between requests or outside the

page

Page 3: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

Page Scope Example/* A simple bean that counts visits. Is found by jsp via the classpath*/

public class Counter {

private int count = 1;

public Counter() {}

public int getCount() { return count++; }

public void setCount(int c) { count = c; }

}

Page 4: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

<%-- Use the Counter bean with page scope. --%>

<jsp:useBean id = "ctr" scope = "page" class = "Counter" />

<html> <head> <title>Page Bean Example</title> </head> <body> <h3>Page Bean Example </h3> <center> <b>The current count for the counter bean is: </b> <jsp:expression> ctr.getCount() </jsp:expression> </center> </body></html>

Page 5: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

The count never changes.

Page 6: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

Request Scope

• One page may call another and the bean is still available.

• When the current request is complete the bean is reclaimed by the JVM.

Page 7: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

Request Scope Example<%-- Use the Counter bean with request scope. --%>

<%@ page errorPage = "errorpage.jsp" %>

<jsp:useBean id = "ctr" scope = "request" class = "Counter" />

<html> <head> <title>Request Bean Example</title> </head> <body> <h3>Request Bean Example </h3>

Page 8: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

<center> <b>Calling another page ... to see if the bean is still there </b> <jsp:scriptlet> ctr.setCount(10); </jsp:scriptlet> </center> <jsp:forward page = "RequestBean2.jsp" /> </body></html>

Page 9: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

<%-- Use the Counter bean with request scope. --%>

<%@ page errorPage = "errorpage.jsp" %><%@ page import = "java.util.*" %>

<jsp:useBean id = "ctr" scope = "request" class = "Counter" />

<html> <head> <title>Request Bean Example Number 2</title> </head>

Page 10: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

<body> <h3>Request Bean Example Number 2</h3> <center> <b>The current count for the counter bean is: </b> <jsp:expression> ctr.getCount() </jsp:expression> <p> <jsp:expression> new Date() </jsp:expression> </center> </body></html>

Page 11: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

Looks like first page hit.

Bean holdsits valueTime changeson each hitbut 10 stays.

Unknown

Page 12: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

Session Scope

Beans with session scope are accessible within pages processingrequests that are in the same session as the one in which thebean was created.

Session lifetime is typically configurable and is controlled bythe servlet container (in our case, Jigsaw).

When the same browser is used, you get the same session bean.

Page 13: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

Session Scope Example<%-- Use the Counter bean with session scope. --%><%@ page errorPage = "errorpage.jsp" %><jsp:useBean id = "ctr" scope = "session" class = "Counter" /><html> <head> <title>Session Bean Example 1</title> </head> <body> <h3>Session Bean Example 1</h3> <center> <b>The current count for the counter bean is: </b> <jsp:expression> ctr.getCount() </jsp:expression> </center> </body></html>

Page 14: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

The counter increments on each hit.

Netscape visits16 times.

Page 15: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

A visit by IE5 changes the count back to one.

Page 16: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

Application Beans

A bean with a scope value of application has an even broaderand further reaching availability than session beans.

Application beans exist throughout the life of the JSP containeritself, meaning they are not reclaimed until the server is shutdown.

Session beans are available on subsequent requests from the samebrowser. Application beans are shared by all users.

Page 17: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

Application Bean Example 1<%-- Use the Counter bean with application scope. --%><%@ page errorPage = "errorpage.jsp" %><jsp:useBean id = "ctr" scope = "application" class = "Counter" /><html> <head> <title>Application Bean Example 1</title> </head> <body> <h3>Application Bean Example 1</h3> <center> <b>The current count for the counter bean is: </b> <jsp:expression> ctr.getCount() </jsp:expression> </center> </body></html>

Page 18: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

Application Bean Example 2<%-- Use the Counter bean with application scope. --%><%-- applicationbean2.jsp --%><%@ page errorPage = "errorpage.jsp" %><%@ page import = "java.util.*" %>

<jsp:useBean id = "ctr" scope = "application" class = "Counter" />

<html> <head> <title>Application Bean Example Number 2</title> </head>

Page 19: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

<body> <h3>Application Bean Example Number 2</h3> <center> <b>We have had </b> <jsp:expression> ctr.getCount() </jsp:expression> <p> <b> total visitors. </b> </center> </body></html>

Page 20: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

After ten visits to applicationBean1.jsp from IE5…

Page 21: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

And then later to applicationbean2.jsp from a differentmachine using Netscape…

Page 22: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

A Shopping Cart

AddToShoppingCart.jsp

Page 23: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

ShoppingCart.jsp

Page 24: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

The Bean – ShoppingCart.java

// ShopingCart.java

import java.util.*;

public class ShoppingCart {

protected Hashtable items = new Hashtable();

public ShoppingCart() {}

Page 25: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

public void addItem(String itemId, String description, float price, int quantity) {

// pack the item as an array of Strings String item[] = { itemId, description, Float.toString(price), Integer.toString(quantity)};

// if item not yet in table then add it if(! items.containsKey(itemId)) {

items.put(itemId, item); } else { // the item is in the table already String tempItem[] = (String[])items.get(itemId); int tempQuant = Integer.parseInt(tempItem[3]); quantity += tempQuant; tempItem[3] = Integer.toString(quantity); } }

Page 26: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

public void removeItem(String itemId) {

if(items.containsKey(itemId)) {

items.remove(itemId); } } public void updateQuantity(String itemId, int quantity) {

if(items.containsKey(itemId)) {

String[] tempItem = (String[]) items.get(itemId); tempItem[3] = Integer.toString(quantity); } } public Enumeration getEnumeration() {

return items.elements(); }

Page 27: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

public float getCost() {

Enumeration enum = items.elements(); String[] tempItem; float totalCost = 0.00f;

while(enum.hasMoreElements()) {

tempItem = (String[]) enum.nextElement(); totalCost += (Integer.parseInt(tempItem[3]) * Float.parseFloat(tempItem[2])); } return totalCost; }

Page 28: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

public int getNumOfItems() {

Enumeration enum = items.elements(); String tempItem[]; int numOfItems = 0; while(enum.hasMoreElements()) {

tempItem = (String[]) enum.nextElement(); numOfItems += Integer.parseInt(tempItem[3]); } return numOfItems; }

Page 29: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

public static void main(String a[]) {

ShoppingCart cart = new ShoppingCart(); cart.addItem("A123", "Bike", (float)432.46, 10); cart.addItem("A124", "Bike", (float)732.46, 5); System.out.println(cart.getNumOfItems()); System.out.println(cart.getCost()); cart.updateQuantity("A123", 2); System.out.println(cart.getNumOfItems()); cart.addItem("A123", "Bike", (float)432.46, 4); System.out.println(cart.getNumOfItems()); }

} C:\Jigsaw\Jigsaw\Jigsaw\WWW\beans>java ShoppingCart157986.9004711

Page 30: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

AddToShoppingCart.jsp

<%@ page errorPage = "errorpage.jsp" %>

<jsp:useBean id = "cart" scope = "session" class = "ShoppingCart" />

<html>

<head>

<title>DVD Catalog </title>

</head>

Page 31: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

<jsp:scriptlet>

String id = request.getParameter("id");

if(id != null) {

String desc = request.getParameter("desc"); Float price = new Float(request.getParameter("price"));

cart.addItem(id, desc, price.floatValue(), 1); }

</jsp:scriptlet>

<a href = "ShoppingCart.jsp"> Shopping Cart Quantity </a>

<jsp:expression> cart.getNumOfItems() </jsp:expression>

<hr>

Page 32: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

<center>

<h3> DVD Catalog </h3>

</center>

<table border = "1" width = "300" cellspacing = "0" cellpadding = "2" align = "center" >

<tr>

<th> Description </th> <th> Price </th>

</tr>

Page 33: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

<tr>

<form action = "AddToShoppingCart.jsp" method = "post" >

<td>Happy Gilmore</td> <td>$19.95</td> <td> <input type = "submit" name = "submit" value = "add"> </td> <input type = "hidden" name = "id" value = "1" > <input type = "hidden" name = "desc" value = "Happy Gilmore" > <input type = "hidden" name = "price" value = "10.95" > </form> </tr>

Page 34: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

<tr>

<form action = "AddToShoppingCart.jsp" method = "post" >

<td>Brassed Off Full Monty</td> <td>$23.99</td> <td> <input type = "submit" name = "submit" value = "add"> </td> <input type = "hidden" name = "id" value = "2" > <input type = "hidden" name = "desc" value = "Brassed Off Full Monty" > <input type = "hidden" name = "price" value = "12.99" > </form> </tr>

Page 35: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

<form action = "AddToShoppingCart.jsp" method = "post" >

<td>FlashDance</td> <td>$12.95</td> <td> <input type = "submit" name = "submit" value = "add"> </td> <input type = "hidden" name = "id" value = "3" > <input type = "hidden" name = "desc" value = "FlashDance" > <input type = "hidden" name = "price" value = "17.05" > </form> </tr> </table> </body><html>

h

Page 36: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

ShoppingCart.jsp<%@ page errorPage = "errorpage.jsp" %><%@ page import = "java.util.*" %><jsp:useBean id = "cart" scope = "session" class = "ShoppingCart" />

<html>

<head>

<title> Shopping Cart Contents </title>

</head>

Page 37: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

<body>

<center>

<table width = "300" border = "1" cellspacing = "0" cellpadding = "2" border = "0" >

<caption>

<b> Shopping Cart Contents </b>

</caption>

<tr> <th> Description </th> <th> Price </th> <th> Quantity </th> </tr>

Page 38: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

<jsp:scriptlet>

Enumeration enum = cart.getEnumeration(); String tempItem[];

while(enum.hasMoreElements()) {

tempItem = (String[]) enum.nextElement();

</jsp:scriptlet>

Page 39: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

<tr> <td> <jsp:expression> tempItem[1] </jsp:expression> </td> <td align = "center"> <jsp:expression> "$" + tempItem[2] </jsp:expression> </td> <td align = "center"> <jsp:expression> tempItem[3] </jsp:expression> </td> </tr>

Page 40: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

<jsp:scriptlet> } </jsp:scriptlet> </table> </center> <a href = "AddToShoppingCart.jsp">Back to Catalog </a> </body></html>

Page 41: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

JSP and XML

<?xml version = "1.0" ?><item> <id>33445</id> <description>The Art of Computer Programming Vol. 1 </description> <price>34.95</price> <quantity>56</quantity></item>

item.xml

Page 42: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.
Page 43: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

A SAX Handler

import java.io.*;import java.util.Hashtable;import org.xml.sax.*;

public class SAXHandler extends HandlerBase {

private Hashtable table = new Hashtable(); private String currentElement = null; private String currentValue = null; public void setTable(Hashtable table) { this.table = table; }

Page 44: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

public Hashtable getTable() { return table; } public void startElement(String tag, AttributeList attrs) throws SAXException { currentElement = tag; } public void characters(char ch[], int start, int length) throws SAXException { currentValue = new String(ch,start,length); } public void endElement(String name) throws SAXException { if(currentElement.equals(name)) { table.put(currentElement, currentValue); } }}

Page 45: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

XMLExample.jsp

<html><head><title>JSP XML Example </title></head><body><%@ page import="java.io.*" %><%@ page import="java.util.Hashtable" %><%@ page import="org.w3c.dom.*" %><%@ page import="org.xml.sax.*" %><%@ page import="javax.xml.parsers.SAXParserFactory" %><%@ page import="javax.xml.parsers.SAXParser" %><%@ page import="SAXHandler" %>

Page 46: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

<jsp:scriptlet>

File file = new File("c:\\Jigsaw\\Jigsaw\\Jigsaw\\Www\\fpml\\item.xml");FileReader reader = new FileReader(file);

SAXParserFactory spf = SAXParserFactory.newInstance();

SAXParser sp = spf.newSAXParser();

SAXHandler handler = new SAXHandler();

sp.parse(new InputSource(reader), handler); // Parse

Hashtable cfgTable = handler.getTable(); //After all the parsing

</jsp:scriptlet>

Page 47: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

<table align="center" width="600"><caption>XML Item From JSP</caption><jsp:scriptlet> // Print the config settings that we are insterested in. out.println("<tr><td align=\"left\">ID</td>" + "<td align=\"center\">" + (String)cfgTable.get(new String("id")) + "</td></tr>"); out.println("<tr><td align=\"left\">DESCRIPTION</td>" + "<td align=\"center\">" + (String)cfgTable.get(new String("description")) + "</td></tr>"); out.println("<tr><td align=\"left\">PRICE</td>" + "<td align=\"center\">" + (String)cfgTable.get(new String("price")) + "</td></tr>"); System.out.println("<tr><td align=\"left\">QUANTITY</td>" + "<td align=\"center\">" + (String)cfgTable.get(new String("quantity")) + "</td></tr>");</jsp:scriptlet>

Page 48: Week 6 JSP’s and JavaBean Page Scope JSP’s and JavaBean Request Scope JSP’s and JavaBean Session Scope JSP’s and JavaBean Application Scope A Shopping.

</table></body></html>