Sun Java logo     Previous      Contents      Index      Next     

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

Chapter 13
Converting Providers to Portlets

This chapter includes instructions for converting the providers to JSR168 compatible portlets using a sample set of instructions for converting the HelloWorldProviderPA in Chapter 7, "Extending the Base Providers" into a portlet called HelloWorldPortlet.

This chapter contains the following sections:

Developing the Class File

Code Example 13-1 contains the HelloWorldPortlet class file. The HelloWorldPortlet, similar to the HelloWorldProviderPA, prints “Hello World!” on the channel.

Code Example 13-1 File  

package examples;



import javax.portlet.GenericPortlet;

import javax.portlet.PortletException;

import javax.portlet.RenderRequest;

import javax.portlet.RenderResponse;

public class HelloWorldPortlet extends GenericPortlet {

    public void doView(RenderRequest request,RenderResponse response)

        throws PortletException, IOException {


        PrintWriter writer = response.getWriter();

        writer.write("Hello World");



Mapping ProviderAdapter to GenericPortlet

When developing the class file, use the following table comparing GenericPortlet to ProviderAdapter:

Table 13-1  Mapping ProviderAdapter to GenericPortlet  



public void init(String n, HttpServletRequest req)

public void init (PortletConfig config)

public URL processEdit (HttpServletRequest request,HttpServletResponse response)

public void processAction (ActionRequest request,ActionResponse response)


public void render (RenderRequest request, RenderResponse resposne)

public StringBuffer getContent (HttpServletRequest request, HttpServletResponse response)

protected void doView (RenderRequest request,RenderResponse response)

public StringBuffer getEdit (HttpServletRequest request, HttpServletResponse response)

protected void doEdit (RenderRequest request,RenderResponse response)

public URL getHelp(HttpServletRequest req, String key)

protected void doHelp (RenderRequest request, RenderResponse response)

public String getName()

public String getPortletName ()

public String getTitle()

protected String getTitle(RenderRequest request)

public ResourceBundle getResourceBundle(String base)

public ResourceBundle getResourceBundle()

public java.util.ResourceBundle getResourceBundle (java.util.Locale locale)


public PortletConfig getPortletConfig ()


public PortletContext getPortletContext ()


public String getInitParameter(java.lang.String name)


public java.util.Enumeration getInitParameterNames()


public void destroy ()

public int getEditType()


public int getWidth()


public boolean isEditable()


public boolean isPresentable()


public ProviderContext getProviderContext()


Sample Code Fragments for Provider to Portlet Conversion

This section includes some sample code fragments for Provider to Portlet conversion.

Example 1



maps to:

Map ui = (Map)renderRequest.getAttribute("javax.portlet.userinfo");

String firstname = (String)ui.get("firstname");

Example 2



maps to (or can use)

PortletSession.getAttribute(), PortletSession.setAttribute()

Example 3



maps to (or can use)

PortletURL.RenderResponse.createRenderURL(), RenderResponse.createActionURL()

Dispatching to a JSP

PortletRequestDispatcher dispatcher = pContext.getRequestDispatcher("test.jsp");

dispatcher.include(request, response);



In Provider, this method returns a helpURL which can be pointing to a static help file.


For portlets, this method has to be implemented for writing out the help content dynamically.



For ProviderAdapter, this method returns the title from the Display Profile title property.

For portlets, the title is returned by the portlet implementation.


For a provider, the editType display profile property can be specified to determine whether the provider implementation will draw the complete edit page or a subset of it.

For portlets, the editType is always EDIT_COMPLETE and the complete page including the form, the Finish, and the Cancel buttons have to be generated by the portlet. Only the banner and footer are drawn by the PortletEdit.jsp in /etc/opt/SUNWps/desktop/default directory.




maps to

PortletPreferences.getValue(key, default)



maps to

PortletPreferences.getValues(key, default[])

Compiling the Portlet

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

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

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 portlet.xml file that includes the declaration for all the portlets.
  2. Code Example 13-2 contains the portlet.xml file that includes the declaration for the sample HelloWorldPortlet.

    Code Example 13-2  portlet.xml File for the HelloWorldPortlet  

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

    <portlet-app xmlns=""



    xsi:noNamespaceSchemaLocation="" version="1.0">














  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 servlet definition can be incorporated in the web.xml file. Code Example 13-3 contains the web.xml file for the sample HelloWorldPortlet.

    Code Example 13-3  web.xml File for HelloWorldPortlet  

    <?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 file 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 adminpassword -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.