Recipe 23.14 Using the EL to Access JavaBean Properties


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


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


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; import; 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 = "";     private final static String DEFAULT_TO =     "";     private final static String DEFAULT_FROM =        "";     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)){    = to;         } else { = 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="" 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="${}" />  <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 ""). 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

See Also

Chapter 20 on using JavaBeans to handle email; the Jakarta Project's Taglibs site: taglibs /index.html; the Sun Microsystems JSTL information page:; 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.

Java Servlet & JSP Cookbook
Java Servlet & JSP Cookbook
ISBN: 0596005725
EAN: 2147483647
Year: 2004
Pages: 326 © 2008-2017.
If you may any questions please contact us: