简体   繁体   中英

HttpSession is null (randomly) when retrieved from HttpServletRequest using ExternalContext

I have following code to retrieve HttpSession from HttpServletRequest using ExternalContext in our Icefaces(1.8.2) & JSF(1.2) based Liferay environment :

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest httpServletRequest = (HttpServletRequest) (externalContext.getRequestMap().get("com.liferay.portal.kernel.servlet.PortletServletRequest");
HttpSession httpSession = httpServletRequest.getSession();

The above code snippet is working fluently for Liferay 6_1_0_CE_GA1 as httpSession is always populated.

But, the same code in Liferay 6_2_1_CE_GA2 , I am randomly getting httpSession as null . Couldn't figure out, what the difference can be!

I have also, tried to get httpServletRequest using PortalUtil.getOriginalServletRequest and then getting httpSession from it, but that doesn't resolve the issue.

When I need access to HttpServletRequest from a Liferay portlet, I have to use this:

HttpServletRequest httpServletRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest));

renderRequest is an object extended from the PortletRequest class. From your code, you can get the PortletRequest object this way:

PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();

So, the resulting code should be:

PortletRequest portletRequest = (PortletRequest) externalContext.getRequest();
HttpServletRequest httpServletRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(portletRequest));

Hope this helps. Regards!

First of all: Please also try this on 6.2 GA6, which is out for a month now. In case this is a bug it might already be fixed during one of the later versions - the version you're using is almost 2 years old now.

Further, there's nothing that I'm aware of in the specification that allows access to the underlying HttpServletRequest . That's not to say that this is impossible (because it obviously is possible) but to say that behaviour might change, because you're outside of the specs.

I'd recommend to inspect the objects you get with a debugger and check if they're wrappers around the original servlet container's request or the actual container's objects. Also, compare if these requests are directed to the portal (check the URL) or to the portlet itself - as they're deployed in different contexts, they'll have different sessions (or one might have sessions while the other doesn't)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM