Generate output

 

+
Search Tips   |   Advanced Search

 

In the previous example, the Hello World portlet provided markup by using a Java PrintWriter. However, most portlets generate output using JSPs. One exception to this is when the portlet has to transform XML source. In this case, the portlet can use XSLT to generate markup. The following sections describe how JSPs are used by portlets.

 

Use JSPs to generate markup

Below is the JSP for the view page of the jsrHelloJSP.war sample.

    <%@ taglib uri="http://java.sun.com/portlet" 
               prefix="portletAPI" %>

    <%@ page session="false"%>

    <p class="portlet-font">Hello JSP!</p>

Differences between this standard portlet example and the corresponding IBM Portlet API version include...

  • The taglib directive points to the URI for the JSP tag library for the Portlet API Specification.

  • The content type must be set in the Java source using the setContentType() method.

  • Style classes from the WSRP specification are used.

Separate JSPs would exist to provide the user interface for supporting any additional portlet modes, such as edit or help. The basic portlet wizard in RAD allows us to create a portlet that provides JSPs for some of the other modes in which the portlet can be invoked.

The following shows the doView() method provided in the jsrHelloJSP sample.

package com.ibm.wps.samples.jsr;

import javax.portlet.*;
import java.io.*;

public class HelloJSP extends GenericPortlet 
{

  public void init(PortletConfig portletConfig) throws UnavailableException, PortletException
  {
    super.init(portletConfig);
  }

  public void doView(RenderRequest request, RenderResponse response)
                                   throws PortletException, IOException 
  {
    // set return content type     response.setContentType("text.html");

    PortletContext context = getPortletConfig().getPortletContext();

    context.getRequestDispatcher("/jsp/View.jsp").include( request, response);
  }

}

Key differences between this standard portlet example and the corresponding IBM portlet API version include...

  • Standard portlet JSPs are included by a request dispatcher's include() method. IBM portlet JSPs are included by using PortletContext.include() method.

  • The MIME type of the output returned in the response must be set before including the JSP. IBM portlets declare the MIME type in the page directive of the JSP.

There are several points to keep in mind when writing the JSPs:

  1. For consistency in portal look and feel, use the portlet's class specifications in the WSRP specification.

  2. Be sure to include the appropriate tag library to obtain the needed functionality in the JSPs.

  3. Become familiar with the guidelines and best practices for portlet markup. For example, all named elements must be namespace-encoded, using the <portletAPI:encodeNamespace> tag, to avoid clashes with other named elements on the portal page.

  4. Portlet JSPs cannot link directly to resources within the portlet's WAR directory structure.

See the Markup guidelines for more information about good JSP coding practices.

 

Create URLs to portlet resources

Portlet JSPs cannot link directly to content (for example, images, applets, other JSPs, or other resources) within the portlet's WAR directory structure. Instead, they have to use the services of the portlet container to create portlet URLs from which the content can be accessed. Use the encodeURL() method of the PortletResponse to access content within the portlet WAR structure. The following examples are used in the View World portlet samples.

ibmViewWorld.war

<img src='<%=portletResponse.encodeURL("images/earth.jpg")%>' 
     alt="Earth" />

jsrViewWorld.war

For standard portlets, you also have to add the context path of the portlet from the request:

<img src='<%=renderResponse.encodeURL(renderRequest.getContextPath() + 
       "/images/earth.jpg")%>' 
         alt="Earth" />

The String returned by the encodeURL() method returns the relative URL of the content, without the host.name and domain information.

Multimedia example

The following example shows how an audio file can be included in a JSP for a standard portlet.

<object 
   classid='<%=renderResponse.encodeURL(renderRequest.getContextPath() +
   "/audio/WakeUpSong.mp3")%>'
        type="audio/wav"  height="18">
   <param name="controls" value="smallconsole" valuetype="data">
   <param name="autostart" value="true" valuetype="data">
   <param name="controller" value="true" valuetype="data">
</object>

Applet example

The following example shows how an applet can be included in an IBM portlet JSP.

<applet codebase='<%=response.encodeURL("applet")%>' 
        code="MyApplet.class"  height="150">
    <param  name="timeout" value="3600">
    <param  name="border"  value="5">
    <param  name="font"   value="TimesRoman|BOLD|18">
    <param  name="bgcolor" value="ffffff">
</applet>

 

Supporting multiple devices, markups, and languages in IBM portlets

WebSphere Portal supports PC browsers, i-mode and WAP phones, plus the ability to create and add support for other devices and clients, markup types, and languages. The challenge with supporting multiple devices, markups, and languages is to render content differently depending on the characteristics of the client, which is provided in the portlet request. For example, one browser may accept HTML 4.0 while another supports XHMTL 1.0. One WAP device might support four lines with 25 characters while another phone has its own PDA-style interface.

For IBM portlets, the aggregation component of WebSphere Portal allows us to package JSPs and resources in directories that match the client with the request, reducing the amount of programming you have to do to support multiple client types. JSPs that contain mostly text, such as help JSPs, can be translated directly rather than storing strings in a resource bundle. JSPs that do not use resource bundles need to be stored in the appropriate location. When a portlet uses a JSP for rendering of the portlet's content, the portal searches for and selects the proper JSP based on the client type (including browser), markup language, and locale indicated in the request. To include a JSP in a portlet, use the appropriate method:

   // JSR 168 include    context.getRequestDispatcher("/jsp/View.jsp").include( request, response);

   // IBM API include     context.include("/jsp/View.jsp", request, response);

To support multiple markup types and locales, the portlet's JSP's must be packaged in the WAR file using the following directory structure:

jsp_path/markup_type /language _region/client/jspname.jsp 

Where

jsp_path

a path defined by the developer. For example, JSPs can be located in the root of the WAR file or in a jsp directory. However, this path must not include mime-type/language_region_variant . The include() method already locates the correct JSP also in those directories.

markup_type

is either html, wml, or chtml .

language

is the language for this JSP, for example, en, ja, or de.

region

is the country, region, or territory of the JSP, for example, US, UK, or CA.

client

is the type of device. For example, it could indicate a JSP with browser-specific markup, such as ie or ns4. Manage Clients help describes how clients are identified by the portal server.

For example, if the client is using Internet Explorer 5 with language properties are set to English (United States), the method include(/mypath/mytemplate.jsp, portletRequest, portletResponse) would cause the portal server to look for the JSP in the following order.

  1. /mypath/html/ie5/en_US/mytemplate.jsp

  2. /mypath/html/ie5/en/mytemplate.jsp

  3. /mypath/html/ie5/mytemplate.jsp

  4. /mypath/html/en_US/mytemplate.jsp

  5. /mypath/html/en/mytemplate.jsp

  6. /mypath/html/mytemplate.jsp

  7. /mypath/mytemplate.jsp

Standard portlets must provide their own logic for handling requests from multiple locales and markup types.

Content accessed by a portlet JSP (for example, images or HTML pages) using ServletResponse.encodeURL() are not found by portal aggregation. To provide different versions of these resources based on client type, markup, and locale, the portlet must use PortletContext.include().

 

Use JSTL in portlet JSPs

The following example shows how to use JSTL to retrieve translated Strings from a resource bundle in the JSPs.

  • The JSTL tag library is included at the beginning of the JSP.

  • The <fmt:setBundle/> tag indicates the resource bundle to use.

  • The <fmt:message/> tag indicates the key to look for in the resource bundle.

  • For the image source, the encodeURL() method is used in a Java scriptlet.

<%@ taglib prefix="fmt" uri="http://java.sun.com/jstl/fmt" %>
...
<fmt:setBundle basename="nls.reminder"/>
...
<img border='0'
     src='<%=response.encodeURL("task_add.gif")%>'
     title='<fmt:message key="add_reminder"/>'
     alt='<fmt:message key="add_reminder"/>'/>

The JARs required to implement JSTL tags are included with the portal server. You should not package these JARs in the portlet's WAR file.

For more information about JSTL tags, see JavaServer Pages Standard Tag Library and JSP Standard Tag Library 1.1 implementation.

 

IBM portlet API examples for Hello JSP

The following examples are from the ibmHelloJSP.war sample in the portal_server_root/dev/samples directory.

<%@ taglib uri="/WEB-INF/tld/portlet.tld" prefix="portletAPI" %>
<%@ page language="java" contentType="text.html; charset=UTF-8" pageEncoding="UTF-8" session="false" %>

<p class="portlet-font">Hello JSP!</p>

The PortletContext.include() method is used to invoke the JSP for the view mode.

package com.ibm.wps.samples.v4;

import org.apache.jetspeed.portlet.*;
import java.io.*;

public class HelloJSP extends PortletAdapter {

  public void init(PortletConfig portletConfig) throws UnavailableException
  {
    super.init(portletConfig);
  }    

  public void doView(PortletRequest request, PortletResponse response) 
                                    throws PortletException, IOException {

    PortletContext context = getPortletConfig().getContext();
    context.include("/jsp/View.jsp", request, response);
  }

}

 

Related information