DOC PREVIEW
CMU ISM 95733 - JSP and JDBC

This preview shows page 1-2-3-4-5-34-35-36-37-38-69-70-71-72-73 out of 73 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 73 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

JSP and JDBCPage ScopePage Scope ExampleUnder TomcatOne Page May Call AnotherCallee.jspAfter Visiting Caller.jspRequest ScopeRequest Scope Caller.jspRequest Scope Callee.jspAfter Visiting Caller.jspSession ScopeSession Scope ExampleSession Scope ExampleApplication BeansApplication Bean Example 1Application Bean Example 2A Shopping CartThe Bean – ShoppingCart.javaAddToShoppingCart.jspShoppingCart.jspA Simple JSP/JDBC ExampleRegister w/ODBCA Simple JSP/JDBC ExampleIt Works!An Example Using Connection PoolingPooledConnection.javaConnectionPool.javaJDBCPooledExample.jspIt works too!SummaryInternet Technologies 1JSP and JDBC• JSP’s and Scope• A Shopping cart application using JSP andJavaBeans• A Simple JSP/JDBC Example• A JSP/JDBC Example using connection poolingMuch of this lecture is from a book entitled “Pure JSP” by Goodwill publishedby SAMSInternet Technologies 2Page ScopeBeans 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 pageInternet Technologies 3Page Scope Example/* A simple bean that counts visits. */import java.io.*;public class Counter implements Serializable {private int count = 1;public Counter() {}public int getCount() { return count++; }public void setCount(int c) { count = c; }}Internet Technologies 4Under TomcatwebappsmyApplicationSomeFile.jspWEB-INFweb.xmlclassesCounter.javaInternet Technologies 5<%-- Use the Counter bean with page scope. --%><%-- The Counter class must be imported. Its in the WEB-INF/classes directory --%><%@ page import="Counter" %> <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:getProperty name = "ctr" property ="count" /></center></body></html>Internet Technologies 6The count never changes.Internet Technologies 7One Page May Call Another<%-- Caller page Caller.jsp --%><html><head><title>Caller page </title></head><body><h1> Caller page </h1><jsp:forward page = "Callee.jsp" /></body></html>Any response data is cleared and controlpasses to the new page.Internet Technologies 8Callee.jsp<%-- Callee page --%><html><head><title>Callee page </title></head><body><h1> Callee page </h1></body></html>Internet Technologies 9After Visiting Caller.jspInternet Technologies 10Request Scope• One page may call another and the bean is still available.• Its considered one request.• The second page will use an existing bean before creating anew one.• When the current request is complete the bean is reclaimedby the JVM.Internet Technologies 11Request Scope Caller.jsp<%-- Caller page --%><%@ page import="Counter" %> <jsp:useBean id = "ctr" scope = "request" class = "Counter" /><html><head><title>Caller page </title><jsp:setProperty name = "ctr" property = "count" value = "10" /></head><body><h1> Caller page </h1><jsp:forward page = "Callee.jsp" /></body></html>Internet Technologies 12Request Scope Callee.jsp<%-- Callee page --%><%@ page import="Counter" %> <jsp:useBean id = "ctr" scope = "request" class = "Counter" /><html><head><title>Callee page </title></head><body><h1> Callee page </h1><jsp:getProperty name = "ctr" property ="count" /> </body></html>Internet Technologies 13After Visiting Caller.jspInternet Technologies 14Session ScopeBeans 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. Currently, my session ends when thebrowser exits.Multiple copies of the same browser each get their own session bean.Internet Technologies 15Session Scope Example<%-- SessionBeanPage.jsp --%><%@ page import="Counter" %> <jsp:useBean id = "ctr" scope = "session" class = "Counter" /><html><head><title>Session Bean Page </title></head><body><h1> Session Bean Page </h1><B>Visit number<jsp:getProperty name = "ctr" property = "count"/> </B></body></html>Internet Technologies 16Session Scope ExampleThe counter increments on each hit till browser exits. New browserback to 1.Internet Technologies 17Application BeansA 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.Internet Technologies 18Application Bean Example 1<%-- ApplicationBeanPage1.jsp --%><%@ page import="Counter" %> <jsp:useBean id = "ctr" scope = "application" class = "Counter" /><html><head><title>Application Bean Page </title> </head><body><h1> Application Bean Page </h1><B>Visit number <jsp:getProperty name = "ctr“property = "count"/> </B></body></html>Internet Technologies 19Application Bean Example 2<%-- ApplicationBeanPage2.jsp --%><%@ page import="Counter" %> <jsp:useBean id = "ctr" scope = "application" class = "Counter" /><html><head><title>Application Bean Page Two </title></head><body><h1> Application Bean Page Two </h1><B>Visit number <jsp:getProperty name = "ctr“property = "count"/> </B></body></html>Internet Technologies 20After several visits with IE5 we visit with Netscape.Internet Technologies 21After visiting from a different machines with a different browsers,we still keep count.Internet Technologies 22A Shopping CartAddToShoppingCart.jspInternet Technologies 23ShoppingCart.jspInternet Technologies 24The Bean – ShoppingCart.java// Adapted from From James Goodwill Pure JSP// ShopingCart.javaimport java.util.*;public class ShoppingCart implements Serializable {protected Hashtable items = new Hashtable();public ShoppingCart() {}Internet Technologies 25public void addItem(String itemId, String description, float price, int quantity) {// pack the item as an array of StringsString item[] = { itemId, description, Float.toString(price), Integer.toString(quantity)};// if item not yet in table then add itif(! items.containsKey(itemId)) {items.put(itemId, item);}else { // the item is in the table alreadyString tempItem[] = (String[])items.get(itemId);int tempQuant = Integer.parseInt(tempItem[3]);quantity += tempQuant;tempItem[3] = Integer.toString(quantity);}}Get a reference to a hashtable entry.Change it.Internet Technologies


View Full Document

CMU ISM 95733 - JSP and JDBC

Download JSP and JDBC
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view JSP and JDBC and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view JSP and JDBC 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?