Render Parameters Tutorial
Resin 3.0

Features
Installation
Configuration
Web Applications
JSP
Servlets and Filters
Portlets
Databases
Admin (JMX)
Amber
Security
XML and XSLT
XTP
Resources
Performance
Protocols
Third-party
Troubleshooting/FAQ

Generic
Tutorials

Basic

Hello World
Render Parameters
Action
Modes
JSP
Hello World
Basic
Action

Find this tutorial in: /opt/resin/webapps/resin-doc/portlet/tutorial/basic-render
Try the Tutorial

Render parameters are used to maintain state.

This tutorial adds the concept of "identity" and "color" to the basic Hello World example, the identity and color are maintained as render parameters.

  1. Files in this tutorial
  2. Portlets are stateless
  3. Getting the value of a render parameter
  4. Setting the value of a render parameter - createRenderURL()
  5. Values for render parameters are maintained
  6. Portlet URL's from outside the portlet

Files in this tutorial

WEB-INF/classes/example/HelloWorldPortlet.java Portlet
WEB-INF/portlets.xml portlet configuration
WEB-INF/web.xml web-app configuration
index.jsp creating urls to the portal from a regular jsp

Portlets are stateless

Just like Servlets a Portlet does not maintain state within the Portlet object. Many requests, from many different users, may be using an instance of a portlet at the same time.

In this tutorial, the "identity" and "color" are two values to contain the state of the portlet. The portlet provides links to change the color or identity, and maintains the identity and color from request to request.

Getting the value of a render parameter

The value of a render parameter is obtained from the request object using request.getParameter(name).

Getting the value of a render parameter
See it in: WEB-INF/classes/example/HelloWorldPortlet.java

  public void render(RenderRequest request, RenderResponse response)
    throws PortletException, IOException
  {
    
    String identity = request.getParameter("identity");
    if (identity == null)
      identity = "World";

    String color = request.getParameter("color");
    if (color == null)
      color = "silver";
    

    ...

    out.println("Hello, " + identity + ".");
    out.println("Your favorite color is " + color);

    ...

Setting the value of a render parameter - createRenderURL()

The value of a render parameter can be changed on a subsequent request with the use of response.createRenderURL() . response.createRenderURL() returns a PortletURL . The PortletURL is used to set render parameters for the next request, the toString() method is used to generate the appropriate url.

response.createRenderURL()
See it in: WEB-INF/classes/example/HelloWorldPortlet.java


    ...

    // url links to change the name

    PortletURL harryUrl = response.createRenderURL();
    harryUrl.setParameter("identity", "Harry");

    PortletURL ronUrl = response.createRenderURL();
    ronUrl.setParameter("identity", "Ron");

    ...

    out.println("<li><a href='" + harryUrl.toString() + "'>Harry</a>");
    out.println("<li><a href='" + ronUrl.toString() + "'>Ron</a>");

    ...


Values for render parameters are maintained

When createRenderURL() is used to create a PortletURL which is then manipulated, parameters that are not explicitly set retain the value they have for the current request.

For example, if:

request.getParameter("identity").equals("Ron") == true
request.getParameter("color").equals("silver") == true

A render url created like this:

PortletURL url = response.createRenderURL();
url.setParameter("color", "gold");
 ... 
 url.toString() 
 ...

Will result in a subsequent request with:

request.getParameter("identity").equals("Ron") == true
request.getParameter("color").equals("gold") == true

Since "identity" was not set when creating the url, it's value of "Ron" is maintained in the generated url.

Portlet URL's from outside the portlet

The functionality described in this section is implemented in index.jsp.

response.createRenderURL() can only be used from within the portlet itself. Since the PortalServlet is just a servlet, any jsp or servlet can initiate a request to a portlet using a url sent to the browser, or the forward and include mechanism available with method ServletContext.getRequestDispatcher and method ServletRequest.getRequestDispatcher .

The simplest usage of the portal is to provide a straight url to it.

url to the portal
See it in: index.jsp
<a href="<c:url value='/hello'/>">This link</a> 
is a regular link that makes a request to the portlet.

Using getRequestDispatcher() to forward to a portlet
  RequestDispatcher d = application.getRequestDispatcher("/hello");
  d.forward(request, response);

The functionality that is provided by portlets requires some extra work when passing parameters. The PortalContext is used to create the urls.

Using a portal from JSP
See it in: index.jsp
<%
GenericPortalContext portalContext 
  = (GenericPortalContext) application.getAttribute("PortalContext(hello)");

GenericPortletURL malfoyUrl = portalContext.createRenderURL();
malfoyUrl.setParameter("identity", "malfoy");
malfoyUrl.setParameter("color", "lime green");

pageContext.setAttribute("malfoyUrl", malfoyUrl);
%>

<a href="<c:url value="${malfoyUrl}"/>">This link</a>
is a link containing render parameters that was made using 
the PortalContext.

The PortalContext is found as an application attribute with a name that corresponds to the servlet-name used when configuring the PortalServlet.

The "hello" portion of application.getAttribute("PortalContext(hello)") corresponds to the servlet-name:

  <servlet servlet-name="hello" 
           servlet-class="com.caucho.portal.generic.PortalServlet">
    ...
  </servlet>

Try the Tutorial


Hello World
Basic
Action
Copyright © 1998-2003 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.