Beans as Models

If your servlet container also hosts EJBs, you can use Entity and Session Beans, along with ordinary Java classes, to encapsulate the Model portion of your application.

Any Java class can be a "bean" to a JSP if it:

  • Has a no-parameter contstructor.
  • Follows the get() - set() convention.
StyleBean
+ StyleBean()
+ getBlogTitle() : String
+ setBlogTitle(String)
+ getEntryStyle() : String
+ setEntryStyle(String)
+ getStampStyle() : String
+ setStampStyle(String)
+ getTitleStyle() : String
+ setTitleStyle(String)

Beans are used with standard JSP actions:

  • jsp:useBean
  • jsp:getProperty
  • jsp:setProperty

Ordinary Java classes also serve as:

  • Business Delegates
  • Data Access Objects
  • Session Facades
  • Value Objects
package com.grdurand;

public class StyleBean {
    private String titleStyle = "font-family:sans-serif;font-size:...
    private String entryStyle = "font-family:monospace; font-size:...
    private String stampStyle = "font-family:sans-serif;font-size:...
    private String blogTitle = "Your Blog, Sir";

    public StyleBean() {}

    public String getTitleStyle() {
        return titleStyle;
    }
    public void setTitleStyle(String style) {
        titleStyle = style;
    }
    public String getEntryStyle() {
        return entryStyle;
    }
    public void setEntryStyle(String style) {
        entryStyle = style;
    }
    public String getStampStyle() {
        return stampStyle;
    }
    public void setStampStyle(String style) {
        stampStyle = style;
    }
    public String getBlogTitle() {
        return blogTitle;
    }
    public void setBlogTitle(String title) {
        blogTitle = title;
    }
}