resin Hello World Portlet Tutorial tutorial hello The Hello, World tutorial shows the most basic portlet and its configuration in a simple browser page.

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:

Simple portlet web-app configuration

A Portlet is a class that implements . 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.

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"); } }

is a servlet that dispatches to a portlet. In this way, portlets can be used as drop-in replacements for servlets.

<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.

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.

<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>