Hello World Portlet Tutorial
Resin 3.0

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

Tutorials

Basic

Hello World
Render Parameters
Action
Modes
JSP
Basic
Basic
Render Parameters

Find this tutorial in: /usr/local/resin/webapps/resin-doc/portlet/tutorial/basic-hello
Try the Tutorial
The Hello, World tutorial shows the most basic portlet and its configuration in a simple browser page.

  1. Files in this tutorial
  2. Portlet
  3. PortletServlet
  4. Compatibility

Portlets form a design pattern where a Portal servlet combines one or more component Portlets into a web page. Because the portlets are written as components, they can be cleanly written and tested. Applications which currently use many servlet includes might be more cleanly written as portlet applications.

The Portal part of the pattern is a servlet which manages the portlets. In this example, the servlet is a simple "invoker" which calls a single portlet to render the page. Many applications will write their own Portal servlets, using Resin's generic portal library to handle the Portlet API.

The "Hello, world" portlet requires the following configuration:

  • The HelloWorldPortlet.java code
  • web.xml configuration of the Portlet servlet

Files in this tutorial

WEB-INF/classes/example/HelloWorldPortlet.java Simple portlet
WEB-INF/web.xml web-app configuration

Portlet

A Portlet is a class that implements class javax.portlet.Portlet . It is similar to a servlet.

The HelloWorldPortlet in this tutorial sends a Hello, World message to the browser.

The response to the browser is generated by the render() method.

example.HelloWorldPortlet
See it in: WEB-INF/classes/example/HelloWorldPortlet.java
package example;

import java.io.PrintWriter;
import java.io.IOException;

import javax.portlet.GenericPortlet;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import javax.portlet.PortletException;

public class HelloWorldPortlet extends GenericPortlet {
  /**
   * The portlet's main view prints "Hello, World"
   */
  public void doView(RenderRequest request, RenderResponse response)
    throws PortletException, IOException
  {
    PrintWriter out = response.getWriter();

    out.println("Hello, World");
  }
}

PortletServlet

class com.caucho.portal.generic.PortletServlet is a servlet that dispatches to a portlet. In this way, portlets can be used as drop-in replacements for servlets.

WEB-INF/web.xml
See it in: WEB-INF/web.xml
<web-app xmlns="http://caucho.com/ns/resin"
         xmlns:resin="http://caucho.com/ns/resin/core">

  <servlet servlet-name="hello" 
           servlet-class="com.caucho.portal.generic.PortletServlet">
    <init>
      <portlet resin:type="example.HelloWorldPortlet"/>
    </init>
  </servlet>

  <servlet-mapping url-pattern="/hello" servlet-name="hello"/>
</web-app>

Portlets work without modification in all portal products. The PortletServlet is a simple way to get a portlet going, and provides a convenient way to replace servlets with portlets.

This approach is valuable for developers that wish to take advantage of the well defined pattern that portlets provide, without necessarily needing the more complete set of features such as sophisticated rendering and management of multiple portlets per page that a more complete portal provides.

Compatibility

The tutorial takes advantage of Resin's dependency injection features. The PortletServlet also supports the use of init-param's if dependency injection is not available.

init-param WEB-INF/web.xml
<web-app>
  <servlet>
    <servlet-name>hello</servlet-name>
    <servlet-class>com.caucho.portal.generic.PortletServlet</servlet-class>
    <init-param>
      <param-name>portlet-class</param-name>
      <param-value>example.HelloWorldPortlet</param-value>
    </init-param>
  </servlet>

  <servlet-mapping>
    <url-pattern>/hello</url-pattern>
    <servlet-name>hello</servlet-name>
  </servlet-mapping>
</web-app>

Try the Tutorial


Basic
Basic
Render Parameters
Copyright © 1998-2005 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.