Simple Generic Portlet example with Liferay

Portlets are the fragments of a portal application. A Portlet contains different lifecycle stages: init, render, processAction, processEvent, serveResource, destroy. The JSR-168/JSR-286 Portlet specification standards specify these life cycle stages of a simple generic portlet.

Before continuing, take a look at this article to learn how to create a Liferay portlet using the LDS IDE.

We have used Liferay version 6.2 with the Apache Tomcat server version 7 on this article.

Table of Contents

Portlet modes

A portlet can have different modes like VIEW, EDIT, HELP.

  • VIEW: View mode is to render the content to the users.
  • EDIT: Edit mode helps the user edit manage the configurations, and customize the behavior of the portlet.
  • HELP: Help mode should provide helpful information about the portlet.

The Following are the methods javax.portlet.Portlet class contains to support above mentioned portlet modes:

  • init(): This method initializes the portlet.
  • doView():  This method performs required actions before rendering the page for portlet in VIEW mode.
  • doEdit()/ doHelp(): These methods are handler methods for portlets that are in EDIT/HELP mode.
  • processAction(): To handle actions.
  • serveResource(): The method that handles the AJAX calls.
  • processEvent(): This method handles the portlet events.
  • public void destroy(): Removal of a portlet from a page invokes this lifecycle method.

Creating a generic portlet

A portlet that is extending Generic portlet must override the doView() method. Below is the simple Generic Liferay portlet example with VIEW mode.

By default Liferay portlet supports the VIEW mode.

  • Create a Liferay project. Create a new action class and extend GenericPortlet.
  • Change the parameter value of the “<portlet-class>” tag in the portlet.xml file to the newly created action class.
  • Implement the doView() method. We are setting the value of the renderRequest parameter “name” and including the view.jsp page to render the result.
  • The init() is the method used to initialize the portlet. We are getting the “view-template” value from the portlet.xml configuration file and setting it into a variable called “viewJsp” that renders the JSP page for VIEW mode.
  • The “include()” method is a convenient method invoked just before the application renders the portlet page.
  • We are retrieving requestDispatcher from portletContext. We also made sure that portletContext is not null before retrieving the request dispatcher.
  • Liferay, by default, comes with Apache’s “common logging” classes. We also need to add a few imports to enable the logging.

Below is the complete portlet class

package com.genportlet;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import javax.portlet.GenericPortlet;
import javax.portlet.PortletException;
import javax.portlet.PortletRequestDispatcher;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import com.liferay.portal.kernel.log.Log;
public class SamplePortlet extends GenericPortlet {
  public void init() {
     viewJsp = getInitParameter("view-template");
  protected void doView(RenderRequest renderRequest, RenderResponse renderResponse) throws PortletException, IOException
     String name = "ABC";
     renderRequest.setAttribute("name", name);
     try {
        include(viewJsp, renderRequest, renderResponse);
    catch (SystemException e) {
 public void include(String jsp, RenderRequest renderRequest, 
              RenderResponse renderResponse) throws SystemException, 
IOException {
     PortletRequestDispatcher prd = getPortletContext().getRequestDispatcher(jsp);
     if(prd == null) {
        _log.error("Error in adding path..!!");
     else {
       try {
         prd.include(renderRequest, renderResponse);
       catch (PortletException e) {
   protected String viewJsp;
   private static Log _log = LogFactoryUtil.getLog(SamplePortlet.class);

Add the following code to view.jsp page to retrieve the parameter called “name” and display the parameter value.

The “<portlet:definedObjects/>” tag gives access to objects like RenderRequest, RenderResponse, PortletConfig, etc.


<%@page import="com.liferay.portal.kernel.util.ParamUtil"%>
<%@ taglib uri="" prefix="portlet" %>
<portlet:defineObjects />
This is the <b>Test</b> portlet in View mode.
<%String param = ParamUtil.get(request, "name", "");%>
<h1>Hello <%=param%>!!!</h1>
  • Now deploy the portlet and add it to a page. Below is the result:
generic portlet


In this article, we learned how to create a simple generic portlet.

We also learned about different portlet modes and created a simple generic Liferay portlet.

We also learned how to set attributes and retrieve them in the JSP file.