Sun Java logo     Previous      Contents      Index      Next     

Sun logo
Sun Java System Portal Server 6 2004Q2 Developer's Guide 

Chapter 12
Extending the GenericPortlet Abstract Class

This chapter includes instructions for extending the GenericPortlet abstract class. It includes instructions to create a sample portlet named PrefPortlet. In the view mode, this portlet displays a salutation. In the edit mode of PrefPortlet, the user can change that salutation. The modified salutation is saved into the preference to be used for subsequent requests to the portlet.

This chapter contains the following sections:

Developing the Class File

Code Example 12-1 contains the class file for the sample PrefPortlet.

Code Example 12-1 File  

package examples;

import javax.portlet.GenericPortlet;

import javax.portlet.ActionRequest;

import javax.portlet.RenderRequest;

import javax.portlet.ActionResponse;

import javax.portlet.RenderResponse;

import javax.portlet.PortletException;

import javax.portlet.PortletURL;

import javax.portlet.PortletMode;

import javax.portlet.PortletPreferences;

import javax.portlet.WindowState;



public class PrefPortlet extends GenericPortlet {

    public void processAction(ActionRequest request, ActionResponse response) throws PortletException {

        // process the salutation set by the user in the edit mode.

        String salutation = request.getParameter("SALUTATION");

        try {

            PortletPreferences pref = request.getPreferences();

            pref.setValue("salutation", salutation);


        } catch (Exception e) {

            throw new PortletException(e.getMessage());


        // return the user back to the view mode and normal state




    public void doView(RenderRequest request,RenderResponse response) throws PortletException,IOException {

        // displays the salutation stored in the preference.

        PortletPreferences pref = request.getPreferences();

        String salutation = pref.getValue("salutation","");


        PrintWriter writer = response.getWriter();

        writer.write("Hello " + salutation);


    public void doEdit(RenderRequest request,RenderResponse response) throws PortletException,IOException {

        // prompt the user for the salutation.

        PortletURL actionURL = response.createActionURL();


        PrintWriter writer = response.getWriter();

        writer.write("<form method=’post’ action=’" + actionURL.toString());

        writer.write("’><TABLE WIDTH=’100%’><TR><TD ALIGN=’RIGHT’ VALIGN=’TOP’>salutation:</TD><TD ALIGN=’LEFT’><INPUT TYPE=’TEXT’ NAME=’SALUTATION’></TD></TR><TR><TD ALIGN=’RIGHT’>&nbsp;</TD><TD ALIGN=’LEFT’><INPUT TYPE=’SUBMIT’ NAME=’SUB1’ VALUE=’Submit’></TD></TR></TABLE></form>");


    public void doHelp(RenderRequest request, RenderResponse response) throws PortletException {


        try {


            PrintWriter writer = response.getWriter();

            writer.write("Pref Portlet Help<p><p>");

        } catch (IOException e) {

            throw new PortletException("PrefPortlet.doHelp exception", e);




Compiling the Portlet

When compiling a portlet, include the classpath. Ensure that portlet.jar is in your classpath when compiling the portlet. For example, to compile, type:

javac -classpath portal-server-install-root/SUNWps/lib/portlet.jar


This compiled class file must be included in the WAR file that will be deployed on the Portal Server using the pdeploy command.

Creating a Portlet Web Application

Assemble the portlet class file and the XML fragments into a portlet web application. A portlet web application must be packaged as a WAR file. The portlet.xml file must be placed into the same location as the web.xml file.

To create a portlet web application:

  1. Create a complete portlet.xml file that includes the declaration for all the portlets.
  2. Code Example 12-2 contains the portlet.xml file that includes the declaration for the sample portlet. In Code Example 12-2, the XML file includes name, class, and cache information. Since the sample portlet also uses preferences, the preference setting is also included in the portlet.xml file. Also PrefPortlet supports the EDIT as well as HELP mode; so the supported modes are specified in the portlet.xml file.

    Code Example 12-2  portlet.xml File for the PrefPortlet  

    <?xml version="1.0" encoding="UTF-8"?>

    <portlet-app xmlns=""



    xsi:schemaLocation=" /opt/SUNWps/dtd/portlet.xsd" version="1.0">












                <keywords>Hello, world, test</keywords>










  3. Create the web.xml file. A web application also requires a web.xml file.
  4. If you have servlets for your portlet web application, then the servlet definition can be incorporated in the web.xml file. Code Example 12-3 contains the web.xml file for the sample portlet.

    Code Example 12-3  web.xml File for PrefPortlet  

    <?xml version="1.0" encoding="ISO-8859-1"?>

    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "">


        <display-name>Portlet Examples</display-name>


  5. Package everything into a WAR file.
  6. For the sample portlets, create an examples directory where all the files of the WAR will be placed. Then within the examples directory, create the WEB-INF directory. All the portlet files will be placed into the WEB-INF directory. For example, your directory hierarchy may look like this:





  7. Create the WAR file. That is, change directories to examples and type the following command:
  8. jar -cvf examples.war *

Deploying the Application

The Portal Server software includes a deployment tool which will take your WAR and deploy it into the portal server. Before deploying the WAR file, determine where to place the portlets inside the organization.

With the following command, the sample portlets can be deployed into the organization.

pdeploy deploy -u uid -w amAdminPassword -g|-d distinguishedName -p ContainerAdminPassword examples.war

For more information on the pdeploy command, see Portal Server Administration Guide.

Creating Channels from the Deployed Portlets

Once the portlet is deployed, the portal server is aware of the portlet defined in the application. You can start to create channels based on the portlet. To create channels, see Portal Server Administration Guide for more information.

Previous      Contents      Index      Next     

Copyright 2004 Sun Microsystems, Inc. All rights reserved.