Datasheet

The Portlet Interface and the GenericPortlet
The Portlet interface defines the behaviors that all portlets must implement. Typically, you would pre-
fer to extend the
GenericPortlet class to build a portlet, because it provides structures for providing
all of the typical portlet implementation methods, not simply the required ones.
Portlet Life Cycle
Much like servlets, a portlet’s life cycle is managed by the container, and has an init method that is
used to manage the initialization requirements (creating resources, configuring, and so on). Portlets are
not guaranteed to be loaded until needed, unless you configure the container to load them on startup.
The
init method takes an object that implements the PortletConfig interface, which manages initial-
ization parameters and the portlet’s
ResourceBundle. This object can be used to get a reference to the
object that implements the
PortletContext interface.
Portlet developers don’t typically spend a lot of time worrying about the intricacies of portlet container
initialization exceptions, because generally they are thrown, and the developer reacts to them (debug-
ging the circumstance that led to the exception and correcting it if appropriate). However, it is worth
noting that an
UnavailableException is able to specify a time for which the portlet will be unavail-
able. This could be both useful (keeping the portlet container from continuously trying to load the port-
let) and aggravating (Why isn’t the portlet container reloading my portlet?!) to a developer.
The
destroy method provides the opportunity to clean up resources that were established in the init
method. This is analogous to the destroy method in a servlet, and is called once when the container
disposes of the portlet.
Portlet Runtime States
When a portlet is running, it has an associated Preferences object that allows for customization of the
portlet. The initial values of the preferences are those specified in the deployment descriptor, but the
portlet has full programmatic access to its preferences.
When a portlet is placed on a page, a
Preferences object is related to it. The pairing of the portlet and a
Preferences object on a page is known as a portlet window. A page can contain many of the same
portlet windows within its display.
Before you start wondering why all of these
Preferences objects are necessary, realize that this is pro-
viding the capability to perform a major features of a portal customization. While the initial portlet
Preferences object is great for specifying the configuration and runtime state of the portlet, it is
When an exception is thrown in the portlet init method, the destroy method is
guaranteed not to be called. Therefore, if resources are created in the
init method
prior to the exception being thrown, the developer cannot expect the
destroy
method to clean them up, and must handle them in the exception’s catch block.
8
Chapter 1
04 469513 Ch01.qxd 1/16/04 11:04 AM Page 8