Portlet Lifecycle method – init()

Initialization phase of Portlet is represented by init() method. When portlet is deployed, portlet container will destroy any existing instance and create new instance of portlet. At this time, it will call init() method.

Like Servlet, Portlet can initialize any back end resource or to do any one time activity in init() method. In short init() method is place for initialization.

Servlet reads init parameter from web.xml in servlet’s init() method. Similarly Portlet reads init parameters from portlet.xml in init() method. Generally these init parameters are used to define page flow of the portlet.
We will see how lifecylce methods works by creating Liferay MVC portlet. Refer my blog on how to create Liferay MVC portlet. Give project name as portlet-phase-lifecycle and eclipse will append -portlet at the end. 

I gave portlet class as com.opensource.techblog.portlet.PhaseAndLifecyclePortlet. The project structure looks like below screenshot.
Portlet Lifecycle method init - Project structure

As mentioned in Liferay MVC Portlet blog, 

  • GenericPortlet implements Portlet interface.
  • LiferayPortlet extends GenericPortlet and provides additional methods.
  • MVCPortlet is defined by Liferay. It extends LiferayPortlet and provides MVC architecture by providing more additional methods.
  • PhaseAndLifecyclePortlet (our custom portlet) extends MVCPorrtlet.

Lifecycle method init

To understand how lifecycle method init works, we will override it in our custom portlet. There are two versions of init() methods available in Liferay portlet class hierarchy.

init() method available in MVCPortlet

  • Signature:- public void init() throws PortletException
    • This method does not have any parameter. 

init() method available in GenericPortlet

  • Signature :- public void init(PortletConfig config) throws PortletException
    • This method take PortletConfig as a parameter. 
    • PortletConfig object is used to read portlet configuration (defined in portlet.xml). 
    • One of the common use of PortletConig object is to read initialization parameter defined in portlet.xml.
    • PortletConfig is similar to ServletConfig (which is used to read servlet configuration from web.xml).

After Overriding these init methods, our portlet class looks like below code snipped.

package com.opensource.techblog.portlet;

import javax.portlet.PortletConfig;
import javax.portlet.PortletException;

import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.util.bridges.mvc.MVCPortlet;

public class PhaseAndLifecyclePortlet extends MVCPortlet {

 //define log for this class
 private static final Log _log = LogFactoryUtil.getLog(PhaseAndLifecyclePortlet.class.getName());
 //This method is defined in MVCPortlet
 public void init() throws PortletException {
  _log.info(" This is init method without parameter");

 //This method is defined in GenericPortlet
 public void init(PortletConfig config) throws PortletException {
  String viewTemplate = config.getInitParameter("view-template");
  _log.info("Init Parameter of viewTemplatei is ==>"+viewTemplate);
  _log.info(" This is init method with PortletConfig parameter");


  • LogFactoryUtil is a util class provided by liferay to get log for current class.
  • We have overridden both version of init() method and added some logs. These log statements will be displayed in server console when this portlet is deployed.
  • In second version of init() method, we are reading initialization parameter called ‘view-template’ and putting into log. ‘view-template’ init parameter is defined in portlet.xml file and used to define the path of jsp which renders portlet’s output.

After doing this change, deploy the portlet. Make sure the server is up and running. During the Portlet deployment, the portlet container will call init() method and you will see below logs in the server console.

Portlet Lifecycle method init - Porttlet modes and lifecycles
  • As you can see during the deployment of this portlet, the init() method overridden from GenericPortlet is called first followed by init() method overridden from MVCPortlet.
  • This is because GenericPortlet is located at a higher position than LiferayMVCPortlet in the class hierarchy.
  • Logs also shows the initialization parameter’s(view-template) value (/view.jsp) which is defined in portlet.xml file. We can read all initialization parameter in init() method.

Place this portlet on some Liferay page. On refreshing this page, init() methods is not called again. It is only called when the portlet is instantiated by the portlet container (during deployment on the server). This means every time when we deploy the portlet, its init() method is called

Summing Up

  • Portlet lifecycle method init will be called by portlet container during initialization of portlet
  • It’s called just once during the deployment of the portlet.
  • It’s a place where we can define any one-time initialization.
Download Source

I would recommend looking at the index page ‘A Complete Liferay Guide‘ to browse all topics about Liferay.


Recommended For You

About the Author: Nilang

Nilang Patel is a technology evangelist who loves to spread knowledge and helping people in all possible ways. He is an author of two technical books - Java 9 Dependency and Spring 5.0 Projects.

2 Comments to “Portlet Lifecycle method – init()”

  1. hi, I’ve try to run this example (using maven) so I have a different structure of the portlet;
    I have seen that I can deploy the portlet on a page; but i do not see the log in any part either in the catalina.out log either in liferay-portal-6.2-ce-ga2/logs.

    What I’m wrong??

    1. Hi Franz,

      If you are running the sever on your local, you can see the logs in tomcal server console.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.