Learning JSP

JSP and Java Beans

Previous  Front

Java Beans: A Quick Intro

A Java Bean is a class that meets the following requirements:

  1. It must have a 0-ary constructor (i.e. parameterless).
  2. It should not have any public instance variables.
  3. Values of class instance variables visible/accessible to other classes should follow naming conventions.

All standard Java classes can be used as beans.

 

Using beans in JSP Pages

	<jsp:useBean id="name" class="package.class" />

Example

<%@ page language="java" contentType="text/html" %>
<HTML>
 <HEAD>
  <TITLE>JSP: Beans Example 1</TITLE>
 </HEAD>
 <BODY BGCOLOR="WHITE">
  <H1>JSP: Example of using a Bean</H1>
  
  <jsp:useBean id="today" class="java.util.Date" />
  The current date and time is:
  <UL>
   <LI> Date: <jsp:getProperty name="today" property="date" />
   <LI> Month: <jsp:getProperty name="today" property="month" />
   <LI> Year: <jsp:getProperty name="today" property="year" />
   <LI> Hours: <jsp:getProperty name="today" property="hours" />
   <LI> Minutes: <jsp:getProperty name="today" property="minutes" />
  </UL>
 </BODY>
</HTML>

EXERCISE

Enter the above in a file called date.jsp and run it.

Accessing Bean properties

	<jsp:getProperty name="objectName" property="propertyName" %>

Setting Bean Properties

	<jsp:setProperty name="objectName" property="propertyName" value="value" />

Example Bean

   package ecom;
   public class SecretMessage {
      private String message = "No message.";
      public String getMessage() {
         return message;
      }
      public void setMessage(String s) {
         message = s;
      }
   }

Using the SecretMessage Bean

<HTML>
 <HEAD>
  <TITLE>Using the SecretMessage Bean</TITLE>
 </HEAD>
 <BODY BGCOLOR="WHITE">
  <H1>Using the Secret Message Bean</H1>
  <jsp:useBean id="MyMessage" class="ecom.SecretMessage" />
  <OL>
   <LI> Initial Message (using getProperty):
            <I> <jsp:getProperty name="MyMessage" property="message" /> </I>
   <LI> <jsp:setProperty name="MyMessage" property="message"
                         value="Meet me at the Foggy Bottom Metro" />
        Secret message (after setting):
            <I> <jsp:getProperty name="MyMessage" property="message" /> </I>
  </OL>
 </BODY>
</HTML>

 

EXERCISE

Enter the code above and try it. You will need to compile the bean class and place it in the appropriate place for you server. For example, Apache Tomcat requires it to be in the directory

install_dir/webpages/WEB-INF/classes/ecom/SecretMessage.class

Another example of using the SecretMessage bean.

<HTML>
 <HEAD>
  <TITLE>Using the SecretMessage Bean version 2</TITLE>
 </HEAD>
 <BODY BGCOLOR="WHITE">
  <H1>Using the Secret Message Bean (Version 2)</H1>
  <jsp:useBean id="MyMessage" class="ecom.SecretMessage" />
  <OL>
   <LI> Initial Message:
            <I> <jsp:getProperty name="MyMessage" property="message" /> </I>
   <LI> <jsp:setProperty name="MyMessage" property="message"
                         value='<%= request.getParameter("Message") />' />
        Secret message:
            <I> <jsp:getProperty name="MyMessage" property="message" /> </I>
  </OL>
 </BODY>
</HTML>

 

EXERCISE

Enter the code above in a file called Bean2.jsp and try it using the following URL:

	http://server.address/youraccount/Bean2.jsp?Message=Rendezvous+at+10

 

Parameter values (as in form input) can also use the syntax:

	<jsp:setPropoerty name="MyMessage" property="message" param="Message" />

EXERCISE

Replace the line in Bean2.jsp to use the above.

 

For multiple parameters that are matched one-to-one with bean properties, you can also use the syntax:

	<jsp:setPropoerty name="MyMessage" property="*" />

 

For further information, see:

Core Servlets and Java Server Pages, Marty Hall, Prentice Hall, 2000.

Java Server Pages, Hans Bergsten, O'Reilly, 2001

A JSP Tutorial from SUN (http://java.sun.com/products/jsp/docs.html)


Previous  Front

Created by Deepak Kumar (dkumar@acm.org)