This topic is intended for developers who are unfamiliar with portlet development. A simple Java class and JSP are provided.
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.
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>