Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
skyrie lum
How to get HttpSession in doView method.
August 25, 2013 12:28 PM
Answer

skyrie lum

Rank: New Member

Posts: 10

Join Date: May 29, 2009

Recent Posts

Hi,

I am trying to get the httpsession(set by servlet in different portlet) in doView method in another portlet :

HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(request);
HttpSession session = httpReq.getSession(false);
UserInfo userInfo = (UserInfo)session.getAttribute("userInfo");

However the session is always null. Any idea to get the session ?

Thanks.
Ahmed Hasan
RE: How to get HttpSession in doView method.
August 25, 2013 10:52 PM
Answer

Ahmed Hasan

Rank: Expert

Posts: 303

Join Date: April 13, 2007

Recent Posts

Hi Skyrie,

Using the below code, I am able to perfectly get the handle to httpSession inside "doView" method.

 1
 2    @Override
 3    public void doView(RenderRequest renderRequest,
 4            RenderResponse renderResponse) throws IOException, PortletException {
 5        super.doView(renderRequest, renderResponse);
 6       
 7        HttpSession httpSession = PortalUtil.getHttpServletRequest(renderRequest).getSession();
 8       
 9        System.out.println("httpSession ==> " + httpSession);
10    }


You can try this and let me know. More such examples and concepts are covered in my free eBook. You can download a free copy and refer there.

Yours truly,
Ahamed Hasan
Author, Liferay Portlet Cookbook
Victor Zorin
RE: How to get HttpSession in doView method.
August 25, 2013 11:19 PM
Answer

Victor Zorin

Rank: Liferay Legend

Posts: 1176

Join Date: April 14, 2008

Recent Posts

However the session is always null. Any idea to get the session ?


Is it session NULL or the attribute? I would suspect that the attribute is NULL, not the session.

Not sure if a cookbook or the cook can help you there as it repeats the same code that you use... Have a look at <private-session-attributes> element in liferay-portlet.xml, try to set it to false. Also google for 'session sharing' for liferay.

We use HTTPSession and PortletSession in many cases for passing data between servlets, portlets and look-and-feel themes (portal-normal.vm).
Once you get you session sharing settings right, you would not have any problems with your code, as it looks alright.
Manupoti Subrahmanyam
RE: How to get HttpSession in doView method.
August 25, 2013 11:35 PM
Answer

Manupoti Subrahmanyam

LIFERAY STAFF

Rank: New Member

Posts: 8

Join Date: April 12, 2013

Recent Posts

Hi,

According to the JSR168 Spec........
A Portlet Application is also a Web Application. The Portlet Application may contain servlets and JSPs in addition to portlets. Portlets, servlets and JSPs may share information through their session.

The PortletSession must store all attributes in the HttpSession of the portlet application. A direct consequence of this is that data stored in the HttpSession by servlets or JSPs is accessible to portlets through the PortletSession in the portlet application scope.

Conversely, data stored by portlets in the PortletSession in the portlet application scope is accessible to servlets and JSPs through the HttpSession. If the HttpSession object is invalidated, the PortletSession object must also be invalidated by the portlet container. If the PortletSession object is invalidated by a portlet, the portlet container must invalidate the associated HttpSession object.

Well both Portlets and Servlets are loaded using the same classloader.So it's a plain fact that session can be made available in both the contexts.

1) PORTLET_SCOPE : Only available to the particular portlet.
2) APPLICATION_SCOPE: Available to all Portlets and also available in Servlets/JSP etc

So, if you really want to share sessions among Servlets and Portlets...
1) Define the scope of the session attributes as APPLICATION_SCOPE in Portlets....
2) Access the session attributes defined or set in servlets by specifying APPLICATION_SCOPE.. else you will get null.

Example code:

PortletSession session = request.getPortletSession();
UserInfo userInfo = (UserInfo)session.getAttribute("userInfo",PortletSession.APPLICATION_SCOPE);

Regards
Subrahmanyam
skyrie lum
RE: How to get HttpSession in doView method.
August 26, 2013 9:47 AM
Answer

skyrie lum

Rank: New Member

Posts: 10

Join Date: May 29, 2009

Recent Posts

Hi,

It is the same code i m using. However i have tested that if the both the portlet sit on the same portlet application then it will works while it conversely it will not work.
So how if i gonna have alot of portlet that need share the same session object/attribute? And i am talking about httpsession instead of portletsession.

Thanks.
Milen Dyankov
RE: How to get HttpSession in doView method.
August 26, 2013 10:24 AM
Answer

Milen Dyankov

LIFERAY STAFF

Rank: Regular Member

Posts: 229

Join Date: October 30, 2012

Recent Posts

Perhaps this post will help you understand why. It refers to a very old Liferay version but the concept is the same.
skyrie lum
RE: How to get HttpSession in doView method.
September 12, 2013 7:28 PM
Answer

skyrie lum

Rank: New Member

Posts: 10

Join Date: May 29, 2009

Recent Posts

In this case, how the theme and the portlets that are in different portlet application share the same httpsession ?
Harish Kumar
RE: How to get HttpSession in doView method.
September 13, 2013 12:13 AM
Answer

Harish Kumar

Rank: Expert

Posts: 342

Join Date: July 31, 2010

Recent Posts

skyrie lum:
Hi,

It is the same code i m using. However i have tested that if the both the portlet sit on the same portlet application then it will works while it conversely it will not work.
So how if i gonna have alot of portlet that need share the same session object/attribute? And i am talking about httpsession instead of portletsession.

Thanks.



As suggested by Manupoti , to share session across portlet application , you need to set the session scope to APPLICATION_SCOPE and make sure <private-session-attributes>false<private-session-attributes> set in liferay-portlet.xml.
robin thakur
RE: How to get HttpSession in doView method.
February 6, 2014 9:07 PM
Answer

robin thakur

Rank: Regular Member

Posts: 126

Join Date: January 9, 2014

Recent Posts

Hi,

HttpSession session = httpReq.getSession(false); here you are using false.
please use this HttpSession session = httpReq.getSession(true); you get session object




Thanks
Praveen Acharya
RE: How to get HttpSession in doView method.
April 15, 2014 10:20 PM
Answer

Praveen Acharya

Rank: New Member

Posts: 1

Join Date: April 15, 2014

Recent Posts

Cool...