Thursday, December 27, 2012

Liferay Session Sharing


I had a requirement for sharing session information from Liferay Portal to Servlet deployed in same Liferay server. so, i developed Liferay portlet which callsPortalSession setParameter method and i store a string value to it. In my servlet, i use request.getSession().getParameter("NAME").

But, unfortunately, i got null value there. (Please note that servlet was a plain servlet and does not contain any liferay code in that war). After doing some reserarching, i found below evidence to achive this ::


For Portlets who will share (i.e setAttribute() ) session attributes(s) need to add following entry in liferay-portlet.xml



By default “LIFERAY_SHARED_”  prefix is used for sharing session attribute to other WARs.  It can be customized with in ‘s session.shared.attributes value.

portletSession.setAttribute( "LIFERAY_SHARED_myVar",value,PortletSession.APPLICATION_SCOPE);

Other portlet(s)  in different WAR  can access it as below :

portletSession.getAttribute( "LIFERAY_SHARED_myVar",PortletSession.APPLICATION_SCOPE);

by implementing above code, i was able to get value stored in one portlet in other portlet which are deployed in different war files.

Now, i added a servlet in portlet where i was getting value from session.

I added below entry in liferay-portlet.xml


and i added below code in my portlet

Object value=portletSession.getAttribute( "LIFERAY_SHARED_myVar",PortletSession.APPLICATION_SCOPE);

 request.getSession().setAttribute("NAME", value);

Now, in my servlet, i was able to get value of NAME session attribute as below ::

Object valueFromSession=request.getSession().getAttribute("NAME");

For knowing more on Private Session xml attribute, refer my next blog post.


Ujjwal Soni

No comments: