Before you begin, you should complete Exercise 1.4: Comparing deployment descriptor differences.
In this exercise, you will learn the differences in JSP file coding between the two portlet APIs. Examine the two versions of the Edit and View JSP files. The basic differences are discussed below.
IBM portlet API tags are declared in the
portlet.tld
tag library. The tags use the portletAPI prefix. The JSR 168 portlet API uses the
std-portlet.tld
tag library and the portlet prefix. Other tag libraries, such as the JavaServer Pages Standard Tag Library (JSTL), defined in
fmt.tld
, can also be used. As shown in the sample code below, the JSTL tag library uses the fmt prefix.
<%@ taglib uri="/WEB-INF/tld/portlet.tld" prefix="portletAPI" %>
<portletAPI:init />
<%@ taglib prefix="fmt" uri="/WEB-INF/tld/fmt.tld" %>
<fmt:setBundle basename="nls.Text" />
<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %>
<portlet:defineObjects />
<%@ taglib prefix="fmt" uri="/WEB-INF/tld/fmt.tld" %>
<fmt:setBundle basename="nls.Text"/>
In the IBM portlet API, the <portletAPI:init> tag makes the PortletRequest, PortletResponse, and PortletConfig objects available to JSP files. In the JSR 168 portlet API, the <portlet:defineObjects> tag makes the RenderRequest, RenderResponse, and PortletConfig objects available to JSP files.
<portletAPI:init />
<portlet:defineObjects />
The two APIs differ in how they set the MIME type for the render response. IBM portlets declare the MIME type on the page directive of the JSP file. JSR 168 portlets declare the MIME type using the setContentType() method of the RenderResponse object in the render methods (doView(), doEdit()).
<%@ page contentType="text/html"
import="java.util.*,
com.ibm.etools.portal.portletexamples.bookmark.legacy.*,
org.apache.jetspeed.portlet.*"
session="false"%>
response.setContentType("text/html");
References to a portlet, portlet page or portlet resource must be encoded in a portlet URI (JSR 168 uses the term URL). The IBM portlet API uses createURI to point to the calling portlet in the current mode, and createReturnURI to point to the calling portlet in the previous mode. The JSR 168 portlet API creates URLs for the action phase (actionURL) and the render phase (renderURL).
in a JSP file: <portletAPI:createURI/>
<portletAPI:createReturnURI/>
in a Java class: PortletResponse.createURI()
PortletResponse.createReturnURI()
in a JSP file: <portlet:actionURL/>
<portlet:renderURL/>
in a Java class: RenderResponse.createActionURL()
RenderResponse.createRenderURL()
Portlet JSP files must encode URLs that refer to resources in the associated WAR file, such as images, applets, and other JSP files. The JSR 168 portlet API also requires that the context path be included in the URL.
<%= response.encodeURL("images/photo01.jpg") %>
<%= renderResponse.encodeURL(renderRequest.getContextPath() + "/images/photo01.jpg") %>
Namespace encoding, for both Java classes and JSP files, is discussed in Namespace encoding in exercise 1.3.
The example code for both APIs shows use of the JSTL tag <fmt:setBundle>.
This tag refers to a standard Java resource bundle, Text.properties
,
in the JavaSource/nls
directory of the samples. Compare this to resource
bundles that define the portlet.
<fmt:setBundle basename="nls.Text" />
<fmt:setBundle basename="nls.Text" />
Now you are ready to begin Exercise 1.6: Deciding which API to use.