Exercise 1.3: Comparing JavaTM class differences

Before you begin, you should complete Exercise 1.2: Conceptual differences between the APIs.

In this exercise, you will learn differences in Java class coding between the two portlet APIs. Examine the two versions of the BookmarkPortlet Java class. Notice these basic differences between the two APIs:

Importing basic portlet classes

The portlet classes that the two APIs import differ.

IBM portlet API
import org.apache.jetspeed.portlet.*;
JSR 168 portlet API
import javax.portlet.*;

Java class inheritance

The two APIs inherit from different classes. The IBM portlet API extends org.apache.jetspeed.portlet.PortletAdapter which provides a default implementation of the org.apache.jetspeed.portlet.Portlet interface. This Portlet class extends HttpServlet, so IBM portlets are a type of servlet. The JSR 168 portlet API provides a javax.portlet.GenericPortlet class which implements the javax.portlet.Portlet interface.

IBM portlet API
public class BookmarkPortlet extends PortletAdapter implements ActionListener
JSR 168 portlet API
public class BookmarkPortlet extends GenericPortlet

Request and response objects

The names of the request and response objects on the render (JSR 168 API) or service (IBM API) methods, such as doView() and doEdit(), differ. The IBM portlet API uses PortletRequest and PortletResponse objects; the JSR 168 API uses RenderRequest and RenderResponse objects. RenderRequest and RenderResponse extend the PortletRequest and PortletResponse objects, respectively, providing common functionality.

IBM portlet API
public void doEdit(PortletRequest request, PortletResponse response)
JSR 168 portlet API
public void doEdit(RenderRequest request, RenderResponse response)

Including JSP files

The IBM portlet API uses the PortletContext object to include JSP files; the JSR 168 portlet API uses the PortletRequestDispatcher object. The include action invokes the JSP file specified.

IBM portlet API
getPortletConfig().getContext().include(EDIT_JSP, request, response);
JSR 168 portlet API
PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(jspName);
rd.include(request, response);

Portlet data

The IBM portlet API stores user data in a PortletData object. The JSR 168 portlet API stores similar information in a PortletPreferences object.

IBM portlet API
PortletData prefs = portletRequest.getData()
JSR 168 portlet API
PortletPreferences prefs = renderRequest.getPreferences()

Action processing

In the IBM portlet API, the Java class must implement the ActionListener interface by providing an actionPerformed() method. Using the JSR 168 portlet API, the Java class must provide a processAction() method; no listener is needed.

IBM portlet API
public void actionPerformed(ActionEvent event) throws PortletException
JSR 168 portlet API
public void processAction(ActionRequest request, ActionResponse response)

Namespace encoding

Namespace encoding is used to ensure that variables used within a portlet are unique within the portal container. The excerpts below also show namespace encoding methods for use in a JSP file.

IBM portlet API
in a Java class: PortletResponse.encodeNamespace()
in a JSP file:   <portletAPI:encodeNamespace/>
JSR 168 portlet API
in a Java class: RenderResponse.getNamespace()
in a JSP file:   <portlet:namespace/>

Now you are ready to begin Exercise 1.4: Comparing deployment descriptor differences.

Terms of use | Feedback
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.