• Liferay Portlet Session Attribute

    Posted on October 19, 2014 by Hamidul Islam in Liferay.

    Portlet Session

    Once you deploy a portlet, a session will be associated with your portlet WAR. In simple language a portlet session is nothing but a java object where we can set attribute and later one we can retrieve attribute value from the session object. This article explains  the use of portlet session in Liferay Portlet. This is the another way to share data between portlets. So IPC can also be implemented using portlet session.


    Portlet Session Object:

    javax.portlet.PortletSession is the interface which is responsible for portlet session. We don't implement this interface. But the portlet container implements this interface. We can easily create portlet session object from request object.For example renderRequest.getPortletSession()

    Portlet Session Scope:

    There two types of portlet session scope. One is PortletSession.PORTLET_SCOPE and another is PortletSession.APPLICATION_SCOPE. If we set attribute in Portlet Scope session then the attribute is accessible only in the same portlet. But in case of PortletSession.APPLICATION_SCOPE the session attribute is available for the entire application. This means that if in your portlet war you have 3 portlets then all the portlets have access to the PortletSession.PORTLET_SCOPE session object.


    1. Set Attribute in APPLICATION_SCOPE

    PortletSession session = renderRequest.getPortletSession();
    session.setAttribute("attribute-name","attribute-value", PortletSession.APPLICATION_SCOPE);
    

    2. Get Attribute from  APPLICATION_SCOPE

    PortletSession session = renderRequest.getPortletSession();
    String attributeValue = (String) session.getAttribute("attribute-name", PortletSession.APPLICATION_SCOPE);
    

    3. Set Attribute in PORTLET_SCOPE

    PortletSession session = renderRequest.getPortletSession();
    session.setAttribute("attribute-name","attribute-value", PortletSession.PORTLET_SCOPE);
    

    4. Get Attribute from  PORTLET_SCOPE

    PortletSession session = renderRequest.getPortletSession();
    String portletScopeValue = (String) session.getAttribute("attribute-name", PortletSession.PORTLET_SCOPE);
    

    Important!!

    1.  Can we create session object in Process Action?

    Answer:-

    Yes we can create session object in process action like actionRequest.getPortletSession()

    2.  Can we retrieve session attribute in render pahase which is set in action phase?

    Answer:-Yes

    3.  Can we share portlet session in another portlet war?

    Answer:- By default we can not share session across different war files. But we can enable it by adding the below line in liferay-portlet.xml file 

    <private-session-attributes>false</private-session-attributes>

    Note1: We should add the above line for all the portlets where we want to set the attribute and where we want to retrieve the attribute. 

    Note2: If we don't add the above line in the portlet where we want to retrieve the session attribute we wont be able to retrieve the attribute.

    4.  Can we remove session attribute?

    Answer:- Yes we can. We can use either

    session.removeAttribute("attribute-name") or session.removeAttribute("attribute-name",SCOPE);

    Post Tagged with , ,

One Response so far.

  1. Philip John says:

    Can we get the session in a Hook, where there are no requests or responses?

Top
%d bloggers like this:

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close