The Complete Magazine on Open Source

Portlet Development With Liferay

3.74K 8

Portlets are small applications that are rendered in the larger context of a portal. When you view any of the popular portals like Yahoo! or Rediff, you see the home page organised into small boxes displaying mail, news, photos, advertisements and other details. These boxes are called portlets. To develop portlets, there are various competing (and sometimes cooperating) technologies: Google Gadgets; Microsoft Sharepoint Portal; and in the world of Java, we have two specifications—JSR-286 and JSR-168—that govern Portlet development. JSR-286 supersedes JSR-168, and is mostly compatible with its predecessor. JSR-286 is sometimes called the Portlet 2.0 specification. The portlet specification is similar to the Java Servlet specification. Understanding the servlet API would shorten the process of learning Portlet technology.

Portlet containers

Portlet containers house the deployed portlets and render them in the context of the portal. There are various portlet containers in the market. WebLogic Portal and WebSphere Portal are popular proprietary portal servers. In the open source space, we have Liferay, JBoss Portal, OpenPortal, Pluto and others.


After a small evaluation of different Portal servers, I shortlisted Liferay. Here is a list of the winning points:

  • Support for the latest standards and specifications
  • Various innovations in the UI layer
  • An extensive set of pre-build ready-to-deploy portlets
  • A good community
  • Open development


Software prerequisites:

  1. JDK 1.6
  2. Maven 2.1
  3. Liferay 5.2

Knowledge prerequisites—a working knowledge of the following technologies will be helpful:

  1. Servlets
  2. JSP
  3. JSTL and EL

Liferay installation

The only prerequisite to install Liferay is that you should have the Java Development Kit in your system. Liferay downloads are conveniently packaged with various application servers and servlet containers. Download the one that you are comfortable with, and extract it. The installation is then complete. The extracted directory will be called LIFERAY_DIR hereafter.

Writing the first JSR-286 portlet

Let us start with the ubiquitous example, writing the Hello World portlet. The code is available in

Before we jump into coding, let’s set up the project environment. My preferred build and dependency management environment is Maven. Using this, we will create a new project (use mvn archetype:generate). Choose a normal WAR project (option 18 in the menu). For our example, I have given com.liferabook, hello-world and com.liferaybook as the groupId, artifactId and the package, respectively. To the generated pom.xml, add the following dependencies:


Now create your first portlet class:

package com.liferaybook;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;"

public class HelloWorldPortlet extends GenericPortlet {

    protected void doView(RenderRequest request, RenderResponse response)
        throws PortletException, IOException {
    request.setAttribute("message", "Hello World!");
    getPortletContext().getRequestDispatcher("/index.jsp").include(request, response);


The code should be understandable to any servlet programmer. The similarities with the servlet API are listed in Table 1. Note that these similarities are based on API usage; they cannot correspond one-to-one in the functional or operational level.

Table 1: Similarities of portlet APIs with servlet APIs
Servlet API Portlet API
javax.servlet.http.HttpServlet javax.portlet.GenericPortlet
javax.servlet.http.HttpServlet.doGet() javax.portlet.GenericPortlet.doView()
javax.servlet.ServletException javax.portlet.PortletException
javax.servlet.http.HttpServletRequest javax.portlet.RenderRequest
javax.servlet.http.HttpServletResponse javax.portlet.RenderResponse
javax.servlet.RequestDispatcher javax.portlet.PortletRequestDispatcher

Coming back to our code, in the doView() method, we first set the request attribute message with the String “Hello World!”, then call RequestDispatcher to include the index.jsp.

Note, in the Maven structure, you have to place the above code as src/main/java/com/liferaybook/

The index.jsp page:


Using the Unified Expression Language (EL), we print the message request attribute set in the HelloWorldPortlet class. Note that in the JSPs you need not have the <html><head><body> elements defined. This is because your portlet is rendered as a box inside the portal’s theme.

The JSPs are placed in the project location: src/main/webapp/

A couple of more configurations need to be written before we can deploy our brand new portlet. These are portlet.xml and web.xml.

The following is what portlet.xml looks like:

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

        <description>Hello World Portlet</description>
        <display-name>Hello World Portlet</display-name>
            <title>Hello World Portlet</title>
            <short-title>Hello World Portlet</short-title>

Shown below is what web.xml looks like:

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

The portlet.xml code should be self-explanatory. The web.xml is an empty XML with no content. This is required, because we are using EL, and this web.xml specifies the Web app specification to version 2.5, where EL can be used inside JSP.

Place both portlet.xml and web.xml in the project path: src/main/webapp/WEB-INF/.

Build the portlet using the mvn package command. This will generate hello-world.war in your target directory. This is your ready-to-deploy portlet war.

Portlet deployment in Liferay

Start the Liferay server, then copy the hello-world.war to the LIFERAY_DIR/deploy/ directory. Monitor your application server log for successful deployment. Once it is deployed, visit the URL: http://localhost:8080/

You will be able to view the default Liferay Portal page [Figure 1].

Figure 1: Liferay start page

Figure 1: Liferay default page

On to the right, you will see the link “Login as bruno”. Click this to log in as the portal administrator. Click on the menu Welcome Bruno Admin! (top-right of the screen) and select Add Application [Figure 2]. This will open the Add Application panel.

Figure 2: Admin menu for adding portlet application

Figure 2: Admin menu for adding portlet application

Under the Undefined category, you will find the deployed portlet [Figure 3].

Figure 3: Add application

Figure 3: Add application

Just drag and drop it in the screen to deploy your portlet [Figure 4].

Figure 4: Our portlet deployed in Liferay

Figure 4: Our portlet deployed in Liferay

So we have successfully developed a small “Hello World” application for deployment in the Liferay Portlet Container. In forthcoming articles in this series, we will discuss some more advanced concepts of the portlet specification.



Your email address will not be published.