JSP and JSTL Tutorials - Herong's Tutorial Notes
Dr. Herong Yang, Version 3.09, 2006

Using JavaBean Classes

Part:   1  2  3  4  5 

JSP/JSTL Tutorials - Herong's Tutorial Notes © Dr. Herong Yang

Using Cookies

Using JavaBean Classes

HTTP Response Header Lines

Non ASCII Characters

JSTL and Expression Language

File Upload

Execution Context

JSP Elements

JSP Standard Tag Libraries (JSTL)

JSP Custom Tag

... Table of Contents

(Continued from previous part...)

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/>
</body></html>
</jsp:root>

Open this JSP page with IE, you will get:

Line 21: author = Unknown
Line 22: total = double: 9.99
Line 23: size = int: 15
Line 24: size = int: 15 
Line 25: time = 1000000000000

Note that:

  • Line 25 tells us that we can even create a Date object and make it available a JavaBean. Its setTime and getTime methods are used provide the time property.

Refreshing the Loaded Bean Classes

Once a bean class has been used once by a JSP page, it will stay loaded in memory to avoid loading it again when another JSP page uses it. This is good to improve response time, but it is a problem if you changed your bean and wants to push the newer version into the server.

One way to force the server to use the new versions of bean classes is to shut down the server and re-start the server.

But a better way to force the server to use the new versions is to use the Tomcat Manger tool. Here is how to do this:

1. Set up a manager user name and password by adding the following line to \local\jakarta-tomcat-4.1.18\conf\tomcat-users.xml:

<user username="herong" password="yang" roles="manager"/>

2. Shut down and re-start Tomcat server.

3. Request UseBean.jsp with IE.

4. Modify CacheBean.java, compile it, and copy the class file the .\web-inf\classes.

5. Run the "reload" command with IE at http://localhost:8080/manager/reload?path=/. You need enter user name and password created in step 1.

6. Request UseBean.jsp again. You should see the changes made to CacheBean.java in step 4.

Part:   1  2  3  4  5 

Dr. Herong Yang, updated in 2006
JSP and JSTL Tutorials - Herong's Tutorial Notes - Using JavaBean Classes