+

Search Tips   |   Advanced Search

 

Develop the portlet

 

This topic is intended for developers who are unfamiliar with portlet development. A simple Java class and JSP are provided.

 

Developing the source Java class file

The following example shows the Java code for a portlet in its simplest form.

package com.ibm.isclite.samples.basicmodule;

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

public class BasicModule extends GenericPortlet {

  public void doView(RenderRequest request, RenderResponse response) 
              throws PortletException, IOException {
    // Set the MIME type for the render response
    response.setContentType("text/html");

        // Invoke the JSP to render
    PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher("/jsp/basicView.jsp");
    rd.include(request,response);
  }

}



The portlet code must extend the GenericPortlet class and output for the response in the doView() method. Portlets are rendered in different modes. The initial mode when a portlet is called to render is the view mode. The response output is provided by a JSP, which provides markup that can be aggregated into a larger HTML page. The package name in this example is consistent with the console module samples.

 

Developing the JSP

The following shows a portlet JSP for view mode in its simplest form.

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1" session="false" buffer="none"%>
<%@ page import="javax.portlet.*" %>
<%@ taglib uri="http://java.sun.com/portlet" prefix="portletAPI" %>

<portletAPI:defineObjects />

<%
  PortletPreferences prefs = renderRequest.getPreferences();
  String URL = prefs.getValue("website","");
%>

<p><a name="<portletAPI:namespace/>basicAnchor">Basic contents</a></p>

<p>
  <a href="<%=URL%>" target="_blank">
    <img src='<%=renderResponse.encodeURL(renderRequest.getContextPath() + "/images/logo.gif")%>' 
         alt="logo" />
      </a>
</p>
<p><em>Company logo with link.</em></p>


The taglib directive specifies the portlet tags from the Java Portlet Specification. The following tags are used in this example.

namespace

Uniquely qualifies named HTML tags or JavaScript functions to prevent clashes with other portlets on the page using the same name.

defineObjects

Makes the RenderRequest, RenderResponse, and PortletConfig objects available to the JSP. This tag is used in this example so that the image can be rendered using the encodeURL() method of the render response, and to allow the JSP to read preferences in the portlet descriptor.



 

Related information


Developing portlets
Creating the descriptors for the console module
Task overview: Managing portlets