











Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of javabeans, their design conventions, and their use in jsp pages. It covers the benefits of using javabeans, such as easier content generation, stronger separation between content and presentation, higher reusability of code, and simpler object sharing. The document also explains how to instantiate, access, and set properties of javabeans using jsp elements like jsp:usebean, jsp:getproperty, and jsp:setproperty.
Typology: Slides
1 / 19
This page cannot be seen from the preview
Don't miss anything!












Slide 2
cs420 fall 2009
JavaBeans for JSP
Reference
What are JavaBeans?
cs420 fall 2009
Java classes that can be easily reused and composed together into an application Any Java class that follows certain design conventions can be a JavaBeans component
JavaBeans components are Java classes that can be easily reused and composed together into applications. Any Java class that follows certain design conventions can be a JavaBeans component. JavaBeans component design conventions govern the properties of the class and govern the public methods that give access to the properties. JavaServer Pages technology directly supports using JavaBeans components with JSP language elements. You can easily create and initialize beans and get and set the values of their properties And JavaBeans can contain business logic or database access logic.
Slide 4
cs420 fall 2009
JavaBeans maintain internal properties A property can be
A JavaBeans component property can be
Why to use accessor methods?
cs420 fall 2009
You can put constraints on values
public void setSpeed(double newSpeed) { if (newSpeed < 0) { sendErrorMessage(...); newSpeed = Math.abs(newSpeed); } speed = newSpeed; }
If users of your class accessed the fields directly, then they would each be responsible for checking constraints.
Slide 8
Why to use accessor methods?
cs420 fall 2009
// Now using metric units (kph, not mph) public void setSpeed(double newSpeed) { setSpeedInKPH = convert(newSpeed); } public void setSpeedInKPH(double newSpeed) { speedInKPH = newSpeed; }
Why to use accessor methods?
cs420 fall 2009
Slide 10
cs420 fall 2009
public class Currency { private Locale locale; private double amount; public Currency() { locale = null; amount = 0.0; } public void setLocale(Locale l) { locale = l; } public void setAmount(double a) { amount = a; } public String getFormat() { NumberFormat nf = NumberFormat.getCurrencyInstance(locale); return nf.format(amount); } }
In the BookStore application, the JSP pages catalog.jsp, showcart.jsp, and cashier.jsp use the util.Currency JavaBeans component to format currency in a locale‐sensitive manner. The bean has two writable properties, locale and amount, and one readable property, format.
The format property does not correspond to any instance variable, but returns a function of the locale and amount properties.
cs420 fall 2009
<% ShoppingCart cart = (ShoppingCart)session.getAttribute("cart"); // If the user has no cart object as an attribute in Session scope // object, then create a new one. Otherwise, use the existing // instance. if (cart == null) { cart = new ShoppingCart(); session.setAttribute("cart", cart); } %> versus <jsp:useBean id="cart" class="cart.ShoppingCart“ scope="session"/>
Now compare the two JSP codes. As you can tell JavaBeans based JSP code is a lot more simpler and easy to understand and would be easier to maintain. In other words, for web page authors, using JavaBeans syntax is a lot easier than writing Java code.
Why Use JavaBeans in JSP Page?
cs420 fall 2009
So just to summarize why you want to use JavaBeans over pure Java code, there are several reasons. First, page authors do not have to know Java programming language in order to use JavaBeans in JSP page because usage syntax of JavaBeans is very simple. Because content generation or business logic is captured in the form of JavaBeans component, it provides a stronger separation between content and presentation. And a single JavaBean can be in fact reusable in other JSP pages. JavaBeans also make it easier to share objects among multiple JSP pages or between requests. Finally JavaBeans greatly simplify the process of reading request parameters, converting from strings, and stuffing the resuts inside objects.
Slide 15
cs420 fall 2009
jsp:useBean
Setting Properties - jsp:setProperty
cs420 fall 2009
Slide 19
cs420 fall 2009
Beans installed in normal Java directory
cs420 fall 2009
Slide 21
cs420 fall 2009
<jsp:useBean id="stringBean“ class="coreservlets.StringBean" />
cs420 fall 2009
<jsp:setProperty name="bookDB"property="bookId"/> is same as <% //Get the identifier of the book to display String bookId = request.getParameter("bookId"); bookDB.setBookId(bookId); ... %>
The Duke's Bookstore application demonstrates how to use the setProperty element and a scriptlet to set the current book for the database helper bean. For example, bookstore3/web/bookdetails.jsp uses the form: <jsp:setProperty name="bookDB" property="bookId"/> whereas bookstore2/web/bookdetails.jsp uses the form: <% bookDB.setBookId(bookId); %>
Slide 25
Example: jsp:setProperty with Expression
cs420 fall 2009
<jsp:useBean id="currency" class="util.Currency" scope="session"> <jsp:setProperty name="currency" property="locale“ value="<%= request.getLocale() %>"/> </jsp:useBean>
<jsp:setProperty name="currency" property="amount" value="<%=cart.getTotal()%>"/>
The above fragments from the page bookstore3/web/showcart.jsp(of Java WSDP) illustrate how to initialize a currency bean with a Locale object and amount determined by evaluating request‐time expressions. Because the first initialization is nested in a useBean element, it is only executed when the bean is created.
Getting JavaBeans Properties
without Converting it to String
cs420 fall 2009
<% Object o = beanName.getPropName(); %>
<% // Print a summary of the shopping cart int num = cart.getNumberOfItems(); if (num > 0) { %>
If you need to retrieve the value of a property without converting it and inserting it into the out object, you must use a scriptlet: <% Object o = beanName.getPropName(); %> Note the differences between the expression and the scriptlet; the expression has an = after the opening % and does not terminate with a semicolon, as does the scriptlet.
Slide 29
cs420 fall 2009
You can use the scope attribute to specify additional places where bean is stored
cs420 fall 2009
page (<jsp:useBean … scope="page"/> or <jsp:useBean…>)
Slide 31
cs420 fall 2009
session (<jsp:useBean … scope="session"/>)
cs420 fall 2009
Benefits of jsp:useBean
Slide 35
Summary &
Questions?
fall 2009 cs