Portlet performs unique operation in each lifecycle execution. These lifecycles are known as portlet phases. Each portlet phase / lifecycle is represented by respective methods in portlet class.
In Servlet, the servlet container calls service() method to process user request. Portlets are design to place together with other portlets on same page(to generate complete web page), it’s possible that user may not directly interacting with Portlet.
For example, if action method is completed for one portlet then its’ render() method is called along with render() method of all portlets on that page.In this case, other portlets on that page are still generating the response even though user had not directly interacted with them.
Due to this behavior, Portlet specification defines more than one methods to process user request. These methods represent corresponding Portlet Phases.
initially following two portlet phases are defined in JSR-168 (Portlet 1.0) specification
For example, if action method is completed for one portlet then its’ render() method is called along with render() method of all portlets on that page.In this case, other portlets on that page are still generating the response even though user had not directly interacted with them.
Due to this behavior, Portlet specification defines more than one methods to process user request. These methods represent corresponding Portlet Phases.
initially following two portlet phases are defined in JSR-168 (Portlet 1.0) specification
- Render Phase
- Action Phase
Later following two portlet phases are added in JSR-286 (Portlet 2.0) specification.
- Event Phase
- Resource Serving Phase
JSR-168 has 2 phases while JSR-286 has total 4 phases (JSR-286 is super set of JSR-168)
Portlet Phases
Following methods represents these portlet phases. These methods are known as Lifecycle of Portlet.
- init()
- This method will be called when portlet is deployed and instantiated by portlet container.
- render()
- This method will be called to render the content. Represent Render phase.
- processAction()
- This method will be called when any action performed.
- processEvent()
- This method will be called when any event is triggered.
- serveResource()
- This method will be called when any resource is served with resource URL.
- destroy()
- This method will be called when portlet is un-deployed by portlet container.
These lifecycle methods / portlet phases are managed by Portlet Container. Portlet container is responsible for following tasks.
- Loading class of portlet
- Creating and maintaining the portlet instance
- Initializing the Portlet
- Submitting user request to portlet instance
- Destroying portlet instance when it is undeployed.
If you are familiar with servlet, you can easily understand portlet by correlate lifecycle methods of portlet and servlet.
Below diagram describes the lifecycle methods of servlet and portlet.
- There is no direct relation between lifecycle methods of portlet and servet.
- This diagram is just to understand lifecycle methods side by side.
- For both Portlet and Servlet, init() and destroy() methods are called by respective container (Portlet and Servlet container).
- In case of Servlet, all requests are served by service() method
- In case of Portlet, user requests are served by different methods like render(), processAction(), processEvent() and serveResource() based on portlet’s current phase.
Explanation of Portlet’s Lifecycle methods
I have given explanation of each Lifecycle methods in separate blog. Below blogs giving detail understanding of each lifecylce method.
- Portlet init phase / Lifecycle method – init()
- Portlet render phase / Lifecycle method – render()
- Portlet action Phase – Portlet Lifecycle method – processAction()
- Portlet serve resource Phase / Lifecycle method – serveResource()
- Portlet Lifecycle method – processEvent() – Available soon
I would recommend looking at index page ‘A Complete Liferay Guide‘ to browse all topics about liferay.