February 21, 2019
Hot Topics:

Introduction to the Java Portlet Specification

  • September 8, 2005
  • By David DeWolf
  • Send Email »
  • More Articles »

Putting It All Together

Tying Up Loose Ends

The GenericPortlet class provides a default implementation of the Portlet interface. By extending GenericPortlet, portlet developers can take advantage of built-in interpretation of Portlet Modes and default implementations of the lifecycle methods. The Hello World example below leverages the GenericPortlet implementation.

Portlet Tag Libraries

The portlet specification defines tag libraries that are used to develop JSPs that render portlet fragments. This library contains tags that can be used to define portlet objects within a page, generate portlet urls, and generate unique portlet namespaces. The example below utilizes these tag libraries to generate the edit page of the portlet (see example code download).

Hello World!, Portlet Style

In the attached Hello World! example, simple rendering and action processing is demonstrated. When first rendered, doView is invoked by the render method of GenericPortlet. This method will render the default greeting.

public void doView(RenderRequest req, RenderResponse res)
throws IOException {
   PrintWriter out = res.getWriter();
   out.println(getGreeting(req) + " World!");

When the portal's controls are used to switch the portlet mode to edit, the portlet will render the edit screen that allows the user to change their greeting. doEdit is invoked by GenericPortlet's implementation of render. In this method, you utilize a request dispatcher to forward the request to a JSP that will generate this content fragment.

public void doEdit(RenderRequest req, RenderResponse res)
   throws PortletException, IOException {
      PortletRequestDispatcher dispatcher =
      dispatcher.include(req, res);

The JSP page utilizes several portlet tags to generate the action URL that will be used to process this page.

<%@ taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
<portlet:actionURL var="url" portletMode="view"/>
<FORM method="POST" action="<%= url %>">
   <TD><INPUT type="text" name="greeting"
              value="<%= renderRequest.getParameter("greeting") %>"/>
   <TD colspan="2"><INPUT type="submit" value="Save"/></TD>

Finally, upon submission, the container calls the portlet's processAction method. This method sets a render parameter that can be utilized while rendering the portlet.

public void processAction(ActionRequest req, ActionResponse res)
throws PortletException {
   String greeting = getGreeting(req);
   res.setRenderParameter("greeting", greeting);

The full source of the example portlet above is available for download.


The basics of portlet development are very similar to those of servlet development. Web application developers who are aware of the basics of portlet development, including the generation of fragments and the separation of action and render requests, should find portlet development fairly familiar. In my next article, I will address the deployment of portlets within Apache Pluto (the reference implementation of the Java Portlet Specification) and show how using Pluto as a lightweight development environment can increase productivity.


  • The Java Portlet Specification
  • The Java Portlet API

About the Author

David DeWolf has been developing Web applications, portals, and portlet applications for six years. He is a member of the Apache Portals Project Management Committee and an active committer to Apache Pluto, the reference implementation of the Portlet Specification. David currently works at Digital Focus, which provides software development, agile coaching, and IT consulting services to Fortune 1000 and medium-sized businesses. Contact David at author@daviddewolf.com.


Page 2 of 2

Comment and Contribute


(Maximum characters: 1200). You have characters left.



Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.

Thanks for your registration, follow us on our social networks to keep up-to-date