Show TOC Start of Content Area

Background documentation Implementing the Portlet Interface  Locate the document in its SAP Library structure

Each portlet must implement the portlet interface or extend a class that implements the portlet interface. This interface covers the base lifecycle phases of the portlet: initialization, request processing and taking the portlet out of service. The portlet interface consists of the following methods:

      init(PortletConfig config)

Initializes the portlet. This method is called only once after instantiating the portlet. It can be used to create expensive objects or resources used by the portlet.

·        processAction(ActionRequest request, ActionResponse response)

Notifies the portlet that the user has triggered an action on this portlet. Only one action is triggered for each client request. In an action, a portlet can issue a redirect, change its portlet mode or window state, modify its persistent state, or set render parameters.

·        render(RenderRequest request, RenderResponse response)

Generate markup fragment. The render method is called for each portlet on the current page and the portlet produces a markup fragment. Markup fragments depend on the portlet mode or window state, render parameters, request attributes, persistent state, session data, or backend data.

·        destroy()

Notify the portlet for the end of the life cycle. This method allows the portlet to free up resources and update any persistent data that belongs to the particular portlet.

Example

The following code demonstrates a basic HelloWorld portlet. It is a simple portlet that does not handle any action events that can result in changing the Portlet Mode or Window State.

import java.io.IOException;

import java.io.PrintWriter;

 

import javax.portlet.ActionRequest;

import javax.portlet.ActionResponse;

import javax.portlet.Portlet;

import javax.portlet.PortletConfig;

import javax.portlet.PortletException;

import javax.portlet.RenderRequest;

import javax.portlet.RenderResponse;

 

public class HelloWorld implements Portlet {

 

  private PortletConfig config;

  

  /**

   * Does nothing

   */

  public HelloWorld() {

  }

 

  /**

   * Called by the portlet container to indicate to a portlet that the

   * portlet is being placed into service.

   */

  public void init(PortletConfig config) throws PortletException {

    this.config = config;

  }

 

  /**

   * Called by the portlet container to allow the portlet to process

   * an action request. In this simple example we do not have a client request that is 

   * triggered by an action URL, so this method does nothing.

   */

  public void processAction(ActionRequest request, ActionResponse response)

  throws PortletException, IOException  {

      throw new PortletException("processAction method not implemented");

  }

  /**

   * Called by the portlet container to allow the portlet to process

   * a render request. This method sets the title and writes aˆ?Hello worldaˆ? string.

   */

  public void render(RenderRequest request, RenderResponse response)

  throws PortletException, IOException  {

      response.setContentType("text/html");

      PrintWriter out = response.getWriter();

      String title = config.getResourceBundle(request.getLocale()).getString("javax.portlet.title");

      response.setTitle(title);

      out.println("Hellow world!");

  }

 

  /**

   * Called by the portlet container to indicate to a portlet that the

   * portlet is being taken out of service.

   */

  public void destroy() {

    // do nothing

  }

 

}

 

End of Content Area