Week 6 JSPs and Java Bean Page Scope

Week 6 • • • JSP’s and Java. Bean Page Scope JSP’s and Java. Bean Request Scope JSP’s and Java. Bean Session Scope JSP’s and Java. Bean Application Scope A Shopping cart application using JSP and Java. Beans Much of this lecture is from a nice little book entitled “Pure JSP” by Goodwill • JSP and XML

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 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 get. Count() { return count++; } public void set. Count(int c) { count = c; } }

<%-- Use the Counter bean with page scope. --%> <jsp: use. Bean id = "ctr" scope = "page" class = "Counter" /> <html> <head> <title>Page Bean Example</title> </head> <body> <h 3>Page Bean Example </h 3> <center> <b>The current count for the counter bean is: </b> <jsp: expression> ctr. get. Count() </jsp: expression> </center> </body> </html>

The count never changes.

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.

Request Scope Example <%-- Use the Counter bean with request scope. --%> <%@ page error. Page = "errorpage. jsp" %> <jsp: use. Bean id = "ctr" scope = "request" class = "Counter" /> <html> <head> <title>Request Bean Example</title> </head> <body> <h 3>Request Bean Example </h 3>

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

<%-- Use the Counter bean with request scope. --%> <%@ page error. Page = "errorpage. jsp" %> <%@ page import = "java. util. *" %> <jsp: use. Bean id = "ctr" scope = "request" class = "Counter" /> <html> <head> <title>Request Bean Example Number 2</title> </head>

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

Looks like first page hit. Bean holds its value Time changes on each hit but 10 stays. Unknown

Session Scope Beans with session scope are accessible within pages processing requests that are in the same session as the one in which the bean was created. Session lifetime is typically configurable and is controlled by the servlet container (in our case, Jigsaw). When the same browser is used, you get the same session bean.

Session Scope Example <%-- Use the Counter bean with session scope. --%> <%@ page error. Page = "errorpage. jsp" %> <jsp: use. Bean id = "ctr" scope = "session" class = "Counter" /> <html> <head> <title>Session Bean Example 1</title> </head> <body> <h 3>Session Bean Example 1</h 3> <center> <b>The current count for the counter bean is: </b> <jsp: expression> ctr. get. Count() </jsp: expression> </center> </body>

The counter increments on each hit. Netscape visits 16 times.

A visit by IE 5 changes the count back to one.

Application Beans A bean with a scope value of application has an even broader and further reaching availability than session beans. Application beans exist throughout the life of the JSP container itself, meaning they are not reclaimed until the server is shut down. Session beans are available on subsequent requests from the same browser. Application beans are shared by all users.

Application Bean Example 1 <%-- Use the Counter bean with application scope. --%> <%@ page error. Page = "errorpage. jsp" %> <jsp: use. Bean id = "ctr" scope = "application" class = "Counter" /> <html> <head> <title>Application Bean Example 1</title> </head> <body> <h 3>Application Bean Example 1</h 3> <center> <b>The current count for the counter bean is: </b> <jsp: expression> ctr. get. Count() </jsp: expression> </center> </body> </html>

Application Bean Example 2 <%-- Use the Counter bean with application scope. --%> <%-- applicationbean 2. jsp --%> <%@ page error. Page = "errorpage. jsp" %> <%@ page import = "java. util. *" %> <jsp: use. Bean id = "ctr" scope = "application" class = "Counter" /> <html> <head> <title>Application Bean Example Number 2</title> </head>

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

After ten visits to application. Bean 1. jsp from IE 5…

And then later to applicationbean 2. jsp from a different machine using Netscape…

A Shopping Cart Add. To. Shopping. Cart. jsp

Shopping. Cart. jsp

The Bean – Shopping. Cart. java // Shoping. Cart. java import java. util. *; public class Shopping. Cart { protected Hashtable items = new Hashtable(); public Shopping. Cart() {}

public void add. Item(String item. Id, String description, float price, int quantity) { // pack the item as an array of Strings String item[] = { item. Id, description, Float. to. String(price), Integer. to. String(quantity)}; // if item not yet in table then add it if(! items. contains. Key(item. Id)) { items. put(item. Id, item); } else { // the item is in the table already String temp. Item[] = (String[])items. get(item. Id); int temp. Quant = Integer. parse. Int(temp. Item[3]); quantity += temp. Quant; temp. Item[3] = Integer. to. String(quantity); } }

public void remove. Item(String item. Id) { if(items. contains. Key(item. Id)) { items. remove(item. Id); } } public void update. Quantity(String item. Id, int quantity) { if(items. contains. Key(item. Id)) { String[] temp. Item = (String[]) items. get(item. Id); temp. Item[3] = Integer. to. String(quantity); } } public Enumeration get. Enumeration() { return items. elements(); }
![public float get. Cost() { Enumeration enum = items. elements(); String[] temp. Item; float public float get. Cost() { Enumeration enum = items. elements(); String[] temp. Item; float](http://slidetodoc.com/presentation_image_h/21fc0c8c22982153159024b2ef29cbf7/image-27.jpg)
public float get. Cost() { Enumeration enum = items. elements(); String[] temp. Item; float total. Cost = 0. 00 f; while(enum. has. More. Elements()) { temp. Item = (String[]) enum. next. Element(); total. Cost += (Integer. parse. Int(temp. Item[3]) * Float. parse. Float(temp. Item[2])); } return total. Cost; }

public int get. Num. Of. Items() { Enumeration enum = items. elements(); String temp. Item[]; int num. Of. Items = 0; while(enum. has. More. Elements()) { temp. Item = (String[]) enum. next. Element(); num. Of. Items += Integer. parse. Int(temp. Item[3]); } return num. Of. Items; }
![public static void main(String a[]) { Shopping. Cart cart = new Shopping. Cart(); cart. public static void main(String a[]) { Shopping. Cart cart = new Shopping. Cart(); cart.](http://slidetodoc.com/presentation_image_h/21fc0c8c22982153159024b2ef29cbf7/image-29.jpg)
public static void main(String a[]) { Shopping. Cart cart = new Shopping. Cart(); cart. add. Item("A 123", "Bike", (float)432. 46, 10); cart. add. Item("A 124", "Bike", (float)732. 46, 5); System. out. println(cart. get. Num. Of. Items()); System. out. println(cart. get. Cost()); cart. update. Quantity("A 123", 2); System. out. println(cart. get. Num. Of. Items()); cart. add. Item("A 123", "Bike", (float)432. 46, 4); System. out. println(cart. get. Num. Of. Items()); } } C: JigsawJigsawWWWbeans>java Shopping. Cart 15 7986. 9004 7 11

Add. To. Shopping. Cart. jsp <%@ page error. Page = "errorpage. jsp" %> <jsp: use. Bean id = "cart" scope = "session" class = "Shopping. Cart" /> <html> <head> <title>DVD Catalog </title> </head>

<jsp: scriptlet> String id = request. get. Parameter("id"); if(id != null) { String desc = request. get. Parameter("desc"); Float price = new Float(request. get. Parameter("price")); cart. add. Item(id, desc, price. float. Value(), 1); } </jsp: scriptlet> <a href = "Shopping. Cart. jsp"> Shopping Cart Quantity </a> <jsp: expression> cart. get. Num. Of. Items() </jsp: expression> <hr>

<center> <h 3> DVD Catalog </h 3> </center> <table border = "1" width = "300" cellspacing = "0" cellpadding = "2" align = "center" > <tr> <th> Description </th> </tr> <th> Price </th>

<tr> <form action = "Add. To. Shopping. Cart. jsp" method = "post" > <td>Happy Gilmore</td> <td>$19. 95</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>

<tr> <form action = "Add. To. Shopping. Cart. jsp" method = "post" > <td>Brassed Off Full Monty</td> <td>$23. 99</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>

<form action = "Add. To. Shopping. Cart. jsp" method = "post" > <td>Flash. Dance</td> <td>$12. 95</td> <input type = "submit" name = "submit" value = "add"> </td> <input type = "hidden" name = "id" value = "3" > <input type = "hidden" name = "desc" value = "Flash. Dance" > <input type = "hidden" name = "price" value = "17. 05" > </form> </tr> </table> </body> <html> h

Shopping. Cart. jsp <%@ page error. Page = "errorpage. jsp" %> <%@ page import = "java. util. *" %> <jsp: use. Bean id = "cart" scope = "session" class = "Shopping. Cart" /> <html> <head> <title> Shopping Cart Contents </title> </head>

<body> <center> <table width = "300" border = "1" cellspacing = "0" cellpadding = <caption> <b> Shopping Cart Contents </b> </caption> <tr> <th> Description </th> <th> Price </th> <th> Quantity </th> </tr>
![<jsp: scriptlet> Enumeration enum = cart. get. Enumeration(); String temp. Item[]; while(enum. has. More. <jsp: scriptlet> Enumeration enum = cart. get. Enumeration(); String temp. Item[]; while(enum. has. More.](http://slidetodoc.com/presentation_image_h/21fc0c8c22982153159024b2ef29cbf7/image-38.jpg)
<jsp: scriptlet> Enumeration enum = cart. get. Enumeration(); String temp. Item[]; while(enum. has. More. Elements()) { temp. Item = (String[]) enum. next. Element(); </jsp: scriptlet>
![<tr> <td> <jsp: expression> temp. Item[1] </jsp: expression> </td> <td align = "center"> <jsp: <tr> <td> <jsp: expression> temp. Item[1] </jsp: expression> </td> <td align = "center"> <jsp:](http://slidetodoc.com/presentation_image_h/21fc0c8c22982153159024b2ef29cbf7/image-39.jpg)
<tr> <td> <jsp: expression> temp. Item[1] </jsp: expression> </td> <td align = "center"> <jsp: expression> "$" + temp. Item[2] </jsp: expression> </td> <td align = "center"> <jsp: expression> temp. Item[3] </jsp: expression> </td> </tr>

<jsp: scriptlet> } </jsp: scriptlet> </table> </center> <a href = "Add. To. Shopping. Cart. jsp">Back to Catalog </a> </body> </html>

JSP and XML item. 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>


A SAX Handler import java. io. *; import java. util. Hashtable; import org. xml. sax. *; public class SAXHandler extends Handler. Base { private Hashtable = new Hashtable(); private String current. Element = null; private String current. Value = null; public void set. Table(Hashtable) { this. table = table; }

public Hashtable get. Table() { return table; } public void start. Element(String tag, Attribute. List attrs) throws SAXException { current. Element = tag; } public void characters(char ch[], int start, int length) throws SAXException { current. Value = new String(ch, start, length); } public void end. Element(String name) throws SAXException { if(current. Element. equals(name)) { table. put(current. Element, current. Value); } }

XMLExample. jsp <html> <head> <title>JSP XML Example </title> </head> <body> <%@ page import="java. io. *" %> <%@ page import="java. util. Hashtable" %> <%@ page import="org. w 3 c. dom. *" %> <%@ page import="org. xml. sax. *" %> <%@ page import="javax. xml. parsers. SAXParser. Factory" %> <%@ page import="javax. xml. parsers. SAXParser" %> <%@ page import="SAXHandler" %>

<jsp: scriptlet> File file = new File("c: \Jigsaw\Jigsaw\Www\fpml\item. xml" File. Reader reader = new File. Reader(file); SAXParser. Factory spf = SAXParser. Factory. new. Instance(); SAXParser sp = spf. new. SAXParser(); SAXHandler handler = new SAXHandler(); sp. parse(new Input. Source(reader), handler); // Parse Hashtable cfg. Table = handler. get. Table(); //After all the parsing </jsp: scriptlet>

<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)cfg. Table. get(new String("id")) + "</td></tr>"); out. println("<tr><td align="left">DESCRIPTION</td>" + "<td align="center">" + (String)cfg. Table. get(new String("description")) + "</td></tr>"); out. println("<tr><td align="left">PRICE</td>" + "<td align="center">" + (String)cfg. Table. get(new String("price")) + "</td></tr>"); System. out. println("<tr><td align="left">QUANTITY</td>" + "<td align="center">" + (String)cfg. Table. get(new String("quantity")) + "</td></tr>"); </jsp: scriptlet>

</table> </body> </html>
- Slides: 48