Liferay Portlet Session

Leave a Comment
Portlet session is similar to HttpSession but both has different implementation.

HttpSession created once for unique visitor in your web application while in Liferay PortletSession is getting created for each portlet placed on page. If there are 4 portlet placed on page then 4 PortletSession would be created for each request.

In Liferay PortletSessionImpl class has implementation for javax.portlet.PortletSession interface. PortletSession internally uses HttpSession to get and set attribute.


There are 2 scope supported in portlet session.
  1. Portlet Scope
  2. Application Scope

Portlet Scope

In PortletSession attributes set using portlet scope would be accessible with in portlet only. If portlet A has set some attribute using portlet scope then no other portlet would be able to access it.

Application Scope

Attribute set using application scope would be accessible across all portlets. If you have set some attribute from portlet A then you would be able to access this attribute in all other portlets as well.

How it works ?

If you see implementation class for PortletSession interface than you will get idea how multiple sessions are getting created for portlet scope.



public void setAttribute(String name, Object value, int scope) {
        if (name == null) {
            throw new IllegalArgumentException();
        }

        if (_invalid) {
            throw new IllegalStateException();
        }

        if (scope == PortletSession.PORTLET_SCOPE) {
            name = _getPortletScopeName(name);
        }

        _session.setAttribute(name, value);
    }

Above method is used to set attribute in portlet session. If you see if scope it portlet scope then it adds namespace before then attribute name while this is not the case with application scope. Here namespace is combination of portlet name and plid.

For example if portletName='47' , plid ='1234 '  and portlet scope is selected then attribute name would be like 'javax.portlet.p.47_LAYOUT_1234?attributeName'.

If scope is application then attribute will be directly stores in HttpSession (_session is object of HttpSession).

Next PostNewer Post Previous PostOlder Post Home

0 comments:

Post a Comment