{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

Info icon This 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"
Image of page 1

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

View Full Document Right 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() {
Image of page 2
return(numItems); } 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> <LINK REL=STYLESHEET HREF="JSP-Styles.css" TYPE="text/css"> </HEAD> <BODY> <CENTER> <TABLE BORDER=5>
Image of page 3

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern