Listing 14 - Listing 14.1 StringBean.java package...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Listing 14.1 StringBean.java package coreservlets; /** A simple bean that has a single String property * called message. */ public class StringBean { private String message = "No message specified"; public String getMessage() { return(message); } public void setMessage(String message) { this.message = message; } } Listing 14.2 StringBean.jsp <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Using JavaBeans with JSP</TITLE> <LINK REL=STYLESHEET HREF="JSP-Styles.css" TYPE="text/css"> </HEAD> <BODY> <TABLE BORDER=5 ALIGN="CENTER"> <TR><TH CLASS="TITLE"> Using JavaBeans with JSP</TABLE> <jsp:useBean id="stringBean" class="coreservlets.StringBean" /> <OL> <LI>Initial value (from jsp:getProperty): <I><jsp:getProperty name="stringBean" property="message" /></I> <LI>Initial value (from JSP expression): <I><%= stringBean.getMessage() %></I> <LI><jsp:setProperty name="stringBean"
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
property="message" value="Best string bean: Fortex" /> Value after setting property with jsp:setProperty: <I><jsp:getProperty name="stringBean" property="message" /></I> <LI><% stringBean.setMessage("My favorite: Kentucky Wonder"); %> Value after setting property with scriptlet: <I><%= stringBean.getMessage() %></I> </OL> </BODY></HTML> Listing 14.3 SaleEntry.java package coreservlets; /** Simple bean to illustrate the various forms * of jsp:setProperty. */ public class SaleEntry { private String itemID = "unknown"; private double discountCode = 1.0; private int numItems = 0; public String getItemID() { return(itemID); } public void setItemID(String itemID) { if (itemID != null) { this.itemID = itemID; } else { this.itemID = "unknown"; } } public double getDiscountCode() { return(discountCode); } public void setDiscountCode(double discountCode) { this.discountCode = discountCode; } public int getNumItems() {
Background image of page 2
} public void setNumItems(int numItems) { this.numItems = numItems; } // In real life, replace this with database lookup. // See Chapters 17 and 18 for info on accessing databases // from servlets and JSP pages. public double getItemCost() { double cost; if (itemID.equals("a1234")) { cost = 12.99*getDiscountCode(); } else { cost = -9999; } return(roundToPennies(cost)); } private double roundToPennies(double cost) { return(Math.floor(cost*100)/100.0); } public double getTotalCost() { return(getItemCost() * getNumItems()); } } Listing 14.4 SaleEntry1.jsp <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Using jsp:setProperty</TITLE>
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/14/2011 for the course MATH 401 taught by Professor H.ayad during the Spring '11 term at Cairo University.

Page1 / 11

Listing 14 - Listing 14.1 StringBean.java package...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online