O'Reilly logo

Java Servlet & JSP Cookbook by Bruce W. Perry

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required