Exercise 1.5: Comparing JSP file coding differences

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.

Tag libraries

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.

IBM portlet API
<%@ 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" />

JSR 168 portlet API
<%@ 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"/>

Making objects available to JSP files

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.

IBM portlet API
<portletAPI:init />
JSR 168 portlet API
<portlet:defineObjects />

MIME type declarations

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()).

IBM portlet API
<%@ page contentType="text/html"
         import="java.util.*,
                 com.ibm.etools.portal.portletexamples.bookmark.legacy.*,
                 org.apache.jetspeed.portlet.*"
         session="false"%>
JSR 168 portlet API
response.setContentType("text/html");

Portlet references

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).

IBM portlet API
in a JSP file:   <portletAPI:createURI/>
                 <portletAPI:createReturnURI/>
in a Java class: PortletResponse.createURI()
                 PortletResponse.createReturnURI()
JSR 168 portlet API
in a JSP file:   <portlet:actionURL/>
                 <portlet:renderURL/>
in a Java class: RenderResponse.createActionURL()
                 RenderResponse.createRenderURL()

URL encoding

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.

IBM portlet API
<%= response.encodeURL("images/photo01.jpg") %>
JSR 168 portlet API
<%= renderResponse.encodeURL(renderRequest.getContextPath() + "/images/photo01.jpg") %>

Namespace encoding

Namespace encoding, for both Java classes and JSP files, is discussed in Namespace encoding in exercise 1.3.

Resource bundles

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.

IBM portlet API
<fmt:setBundle basename="nls.Text" />
JSR 168 portlet API
<fmt:setBundle basename="nls.Text" />

Now you are ready to begin Exercise 1.6: Deciding which API to use.

Feedback
(C) Copyright IBM Corporation 2000, 2005. All Rights Reserved.