Fórumok

How to get HttpSession in doView method.

skyrie lum, módosítva 10 év-val korábban

How to get HttpSession in doView method.

New Member Bejegyzések: 10 Csatlakozás dátuma: 2009.05.30. Legújabb bejegyzések
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.
thumbnail
Ahmed Hasan, módosítva 10 év-val korábban

RE: How to get HttpSession in doView method.

Expert Bejegyzések: 306 Csatlakozás dátuma: 2007.04.13. Legújabb bejegyzések
Hi Skyrie,

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


	@Override
	public void doView(RenderRequest renderRequest,
			RenderResponse renderResponse) throws IOException, PortletException {
		super.doView(renderRequest, renderResponse);
		
		HttpSession httpSession = PortalUtil.getHttpServletRequest(renderRequest).getSession();
		
		System.out.println("httpSession ==> " + httpSession);
	}


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
skyrie lum, módosítva 10 év-val korábban

RE: How to get HttpSession in doView method.

New Member Bejegyzések: 10 Csatlakozás dátuma: 2009.05.30. Legújabb bejegyzések
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.
thumbnail
Milen Dyankov, módosítva 10 év-val korábban

RE: How to get HttpSession in doView method.

Expert Bejegyzések: 310 Csatlakozás dátuma: 2012.10.30. Legújabb bejegyzések
Perhaps this post will help you understand why. It refers to a very old Liferay version but the concept is the same.
skyrie lum, módosítva 10 év-val korábban

RE: How to get HttpSession in doView method.

New Member Bejegyzések: 10 Csatlakozás dátuma: 2009.05.30. Legújabb bejegyzések
In this case, how the theme and the portlets that are in different portlet application share the same httpsession ?
thumbnail
Harish Kumar, módosítva 10 év-val korábban

RE: How to get HttpSession in doView method.

Expert Bejegyzések: 483 Csatlakozás dátuma: 2010.07.31. Legújabb bejegyzések
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.
thumbnail
Victor Zorin, módosítva 10 év-val korábban

RE: How to get HttpSession in doView method.

Liferay Legend Bejegyzések: 1228 Csatlakozás dátuma: 2008.04.14. Legújabb bejegyzések
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.
thumbnail
Manupoti Subrahmanyam, módosítva 10 év-val korábban

RE: How to get HttpSession in doView method.

Junior Member Bejegyzések: 39 Csatlakozás dátuma: 2013.04.12. Legújabb bejegyzések
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
Praveen Acharya, módosítva 10 év-val korábban

RE: How to get HttpSession in doView method.

New Member Bejegyzés: 1 Csatlakozás dátuma: 2014.04.16. Legújabb bejegyzések
Cool...
robin thakur, módosítva 10 év-val korábban

RE: How to get HttpSession in doView method.

Regular Member Bejegyzések: 146 Csatlakozás dátuma: 2014.01.09. Legújabb bejegyzések
Hi,

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




Thanks