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
Embed
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.
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
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 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 getCount() { return count++; }
public void setCount(int c) { count = c; }
}
<%-- 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>
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 errorPage = "errorpage.jsp" %>
<jsp:useBean id = "ctr" scope = "request" class = "Counter" />
<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>
<%-- Use the Counter bean with request scope. --%>
<jsp:useBean id = "ctr" scope = "request" class = "Counter" />
<html> <head> <title>Request Bean Example Number 2</title> </head>
<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>
Looks like first page hit.
Bean holdsits valueTime changeson each hitbut 10 stays.
Unknown
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.
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>
The counter increments on each hit.
Netscape visits16 times.
A visit by IE5 changes the count back to one.
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.
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>
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>
<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>
After ten visits to applicationBean1.jsp from IE5…
And then later to applicationbean2.jsp from a differentmachine using Netscape…
A Shopping Cart
AddToShoppingCart.jsp
ShoppingCart.jsp
The Bean – ShoppingCart.java
// ShopingCart.java
import java.util.*;
public class ShoppingCart {
protected Hashtable items = new Hashtable();
public ShoppingCart() {}
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); } }
public void removeItem(String itemId) {
if(items.containsKey(itemId)) {
items.remove(itemId); } } public void updateQuantity(String itemId, int quantity) {
<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>
<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>
<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>