Simple Generic Portlet example with Liferay

Portlets are the fragments of a web application. A Portlet contains the following lifecycle stages: init, render, processAction, processEvent, serveResource, destroy. These are based on JSR-168/JSR-286 Portlet specification standards.

Before continuing, take a look at creating Liferay portlet using Liferay Developer Studio.

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 to edit and add configurations to 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 is called when a portlet is created.
  • doView():  This is used to render the page for portlet in VIEW mode.
  • doEdit()/ doHelp(): These are invoked when portlets are in EDIT/HELP mode.
  • processAction(): To handle actions.
  • serveResource(): It is used to serve resources without refreshing the page.
  • processEvent(): It is used to handle portlet events.
  • public void destroy(): It is called when the portlet is removed from a page.

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

By default Liferay portlet supports the VIEW method.

  • Create a Liferay project. Create a new action class and extend GenericPortlet.
  • Change the parameter value of <portlet-class> tag in the portlet.xml file to the newly created action class.
  • implement doView() method. In our example below, we are setting renderRequest parameter called “name” and including the view.jsp page to render the result.
  • init() is the method used to initialize portlet. In this example, we are getting “view-template” value from portlet.xml configuration file and setting into a variable called “viewJsp”, which is the render JSP page for VIEW mode.
  • include() is a convenient method used to forward processing to the JSP page. This can be used as a common method to forward processing from any other methods.
  • In the given example we are getting requestDispatcher from portletContext. We also made sure that portletContext is not null before getting the request dispatcher.
  • Liferay by default comes with Apache’s common logging classes. We just need to do a few imports to enable the logging.
  • Complete portlet class is given below.

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. Here we are getting the parameter “name” which was set inside doView() method and displaying it.
  • Here we need to add the tag as it gives several 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:

Note: Version details: Liferay version 6.2 + Apache Tomcat 7 server.

You may also interested in