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:
The portlet classes that the two APIs import differ.
import org.apache.jetspeed.portlet.*;
import javax.portlet.*;
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.
public class BookmarkPortlet extends PortletAdapter implements ActionListener
public class BookmarkPortlet extends GenericPortlet
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.
public void doEdit(PortletRequest request, PortletResponse response)
public void doEdit(RenderRequest request, RenderResponse response)
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.
getPortletConfig().getContext().include(EDIT_JSP, request, response);
PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(jspName);
rd.include(request, response);
The IBM portlet API stores user data in a PortletData object. The JSR 168 portlet API stores similar information in a PortletPreferences object.
PortletData prefs = portletRequest.getData()
PortletPreferences prefs = renderRequest.getPreferences()
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.
public void actionPerformed(ActionEvent event) throws PortletException
public void processAction(ActionRequest request, ActionResponse response)
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.
in a Java class: PortletResponse.encodeNamespace()
in a JSP file: <portletAPI:encodeNamespace/>
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.