[ Team LiB ] Previous Section Next Section

Recipe 23.14 Using the EL to Access JavaBean Properties

Problem

You want to use the EL to access the properties of a JavaBean in a JSP.

Solution

Use the jsp:useBean standard action to create or access an instance of the bean, then use the EL to access the bean properties.

Discussion

You can use the c:out JSTL core tag and the EL to display the values of JavaBean properties in a JSP. Example 23-15 shows the skeleton of a JavaBean that is designed to handle email. I used this bean in Chapter 20, which contains details about all of its email-sending and -accessing methods.

Example 23-15. A JavaBean that a JSP will instantiate and access
package com.jspservletcookbook;    

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class EmailBean  {

    //defaults
    private final static String DEFAULT_SERVER = "smtp.comcast.net";
    private final static String DEFAULT_TO =
    "author@jspservletcookbook.com";

    private final static String DEFAULT_FROM = 
      "author@jspservletcookbook.com";

    private final static String DEFAULT_CONTENT = "Unknown content";
    private final static String DEFAULT_SUBJECT= "Unknown subject";
   

    //JavaBean properties
        private String smtpHost;
        private String to;
        private String from;
        private String content;
        private String subject;
        
    //no-args constructor for the bean
        public EmailBean( ){}
    
     //configure an email message with request params and send the email
     public void sendMessage(HttpServletRequest request,
              PrintWriter out) throws IOException {
     
        //SEE RECIPE 20.3 AND 20.6 FOR MORE DETAILS ON THIS EMAIL BEAN
        //METHOD

    }//sendMessage
        
    //get email messages using a POP account
        private void handleMessages(HttpServletRequest request,
        PrintWriter out) throws IOException, ServletException {
    
       //SEE RECIPE 20.3 AND 20.6 FOR MORE DETAILS ON THIS EMAIL BEAN
       //METHOD

    }//handleMessages
    
  //display info about received email messages
  private void displayMessage(Message msg, PrintWriter out) 
    throws MessagingException, IOException{
    
 //SEE RECIPE 20.3 AND 20.6 FOR MORE DETAILS ON THIS EMAIL BEAN

   }//displayMessage

   //getter or accessor methods

    public String getSmtpHost( ){
      
        return (smtpHost == null || smtpHost.equals("")) ?
          EmailBean.DEFAULT_SERVER : smtpHost;
      
    }//getSmtpHost
    
    public String getTo( ){
      
      return to;
      
    }//getTo
    
    public String getFrom( ){
      
      return from;
      
    }//getFrom
    
    public String getContent( ){
      
      return content;
      
    }//getContent
    
    public String getSubject( ){
      
      return subject;
      
    }//getSubject
    
    //setter or mutator methods

   public void setSmtpHost(String host){
        if (check(host)){
            this.smtpHost = host;
        } else {
    this.smtpHost = EmailBean.DEFAULT_SERVER;
        }
    }//setSmtpHost
    
   public void setTo(String to){
        if (check(to)){
            this.to = to;
        } else {
    this.to = EmailBean.DEFAULT_TO;
        }
    }//setTo
    
   public void setFrom(String from){
        if (check(from)){
            this.from = from;
        } else {
    this.from = EmailBean.DEFAULT_FROM;
        }
    }//setFrom
    
   public void setContent(String content){
        if (check(content)){
            this.content = content;
        } else {
    this.content = EmailBean.DEFAULT_CONTENT;
        }
    }//setContent
    
   public void setSubject(String subject){
        if (check(subject)){
            this.subject = subject;
        } else {
    this.subject = EmailBean.DEFAULT_SUBJECT;
        }
    }//setSubject
    
    private boolean check(String value){
    
        if(value == null || value.equals(""))
            return false;
            
    return true;
    }
}

Example 23-16 shows the JSP that creates an instance of this bean using the jsp:useBean standard action. The id attribute of jsp:useBean specifies "emailer" as the bean name. This is the name the code uses to access the bean instance's property values using the EL.

Example 23-16. Creating a JavaBean and using the JSTL to display its property values
<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>

<jsp:useBean id="emailer" class="com.jspservletcookbook.EmailBean"/>

<jsp:setProperty name="emailer" property="*" />

<html>
<head><title>Bean property display</title></head>
<body>
<h2>Here are the EmailBean properties</h2>

<strong>SMTP host: </strong> <c:out value="${emailer.smtpHost}" /><br />

<strong>Email recipient: </strong> <c:out value="${emailer.to}" /><br />

<strong>Email sender: </strong> <c:out value="${emailer.from}" /><br />

<strong>Email subject: </strong> <c:out value="${emailer.subject}" /><br />

<strong>Email content: </strong> <c:out value="${emailer.content}" /><br />

</body>
</html>

When the code uses an expression such as "${emailer.smtpHost}," it calls the getSmtpHost( ) method of the EmailBean (the SMTP server from which you receive email, such as "smtp.comcast.net"). The variable emailer refers to the instance of the EmailBean.

Example 23-16 set all of the EmailBean's settable properties from request parameters of the same name. This is the purpose of the code:

<jsp:setProperty name=
  "emailer" property="*" />

Providing the c:out value attribute with this expression outputs the value of the bean's property. Figure 23-9 shows the JSP of Example 23-16 in a web browser.

Figure 23-9. Displaying a JavaBean's properties using JSTL c:out tags
figs/jsjc_2309.gif

See Also

Chapter 20 on using JavaBeans to handle email; the Jakarta Project's Taglibs site: http://jakarta.apache.org/taglibs/index.html; the Sun Microsystems JSTL information page: http://java.sun.com/products/jsp/jstl/; Recipe 23.3 on using the core tags; Recipe 23.4 and Recipe 23.5 on using the XML tags; Recipe 23.6 on using the formatting tags; Recipe 23.7 and Recipe 23.8 on using the SQL JSTL tags; Recipe 23.9-Recipe 23.13 on using the EL to access scoped variables, request parameters, request headers, and cookies.

    [ Team LiB ] Previous Section Next Section