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.
I gave portlet class as com.opensource.techblog.portlet.PhaseAndLifecyclePortlet. The project structure looks like below screenshot.
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
@Override
public void init() throws PortletException {
_log.info(" This is init method without parameter");
super.init();
}
//This method is defined in GenericPortlet
@Override
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");
super.init(config);
}
}Explanation:-
- 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.
- 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.
I would recommend looking at the index page ‘A Complete Liferay Guide‘ to browse all topics about Liferay.






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??
Hi Franz,
If you are running the sever on your local, you can see the logs in tomcal server console.
Regards
Nilang