Download Herong`s Notes on JSP

Transcript
http://www.geocities.com/herong_yang/jsp/usebean.html
Line 12 tells us that if we use scriptlet element, we can call a specific version of setTotal
method. Remember setProperty failed on "total" in the previous example.
Line 15 tells us that we also retrieve the object back from pageContext, because useBean
element store the JavaBean object in pageContext.
Using Java Objects as JavaBeans
Now we know that a JavaBean is just a normal Java object with some specially named methods,
and stored in pageContext's attribute collection. We can use the useBean to create a JavaBean
and use it any way we wanted.
The next question is: can we create any object and put it into pageContext's attribute collection,
and use setProperty and getProperty elements? The answer is yes. Here is my sample JSP page
to show you this:
<?xml version="1.0"?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="1.2">
<!-- ObjectAsBean.jsp
Copyright (c) 2003 by Dr. Herong Yang
-->
<html><body>
<jsp:scriptlet><![CDATA[
herong.DemoBean b = new herong.DemoBean();
pageContext.setAttribute("b", b, PageContext.PAGE_SCOPE);
b.setAuthor("Unknown");
b.setTotal(9.99);
]]></jsp:scriptlet>
Line 21: author =
<jsp:getProperty name="b" property="author"/><br/>
Line 22: total =
<jsp:getProperty name="b" property="total"/><br/>
<jsp:scriptlet><![CDATA[b.setSize(15);]]></jsp:scriptlet>
Line 23: size =
<jsp:getProperty name="b" property="size"/><br/>
Line 24: size =
<jsp:scriptlet><![CDATA[out.println(b.getSize());]]></jsp:scriptlet>
<br/>
<jsp:scriptlet><![CDATA[
java.util.Date d = new java.util.Date();
pageContext.setAttribute("d", d, PageContext.PAGE_SCOPE);
]]></jsp:scriptlet>
<jsp:setProperty name="d" property="time" value="1000000000000"/>
Line 25: time =
<jsp:getProperty name="d" property="time"/><br/>
10 of 12