[英]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 : 我有以下代码在Icefaces(1.8.2) & JSF(1.2)
的Liferay环境中使用ExternalContext
从HttpServletRequest
检索HttpSession
:
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. 上面的代码段对于Liferay 6_1_0_CE_GA1
一直Liferay 6_1_0_CE_GA1
因为始终填充httpSession
。
But, the same code in Liferay 6_2_1_CE_GA2
, I am randomly getting httpSession
as null
. 但是,在Liferay 6_2_1_CE_GA2
的相同代码中,我随机将httpSession
为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. 我也,试图让httpServletRequest
使用PortalUtil.getOriginalServletRequest
,然后让httpSession
从它,但这并不解决问题。
When I need access to HttpServletRequest from a Liferay portlet, I have to use this: 当我需要从Liferay Portlet访问HttpServletRequest时,必须使用以下命令:
HttpServletRequest httpServletRequest = PortalUtil.getOriginalServletRequest(PortalUtil.getHttpServletRequest(renderRequest));
renderRequest is an object extended from the PortletRequest class. renderRequest是从PortletRequest类扩展的对象。 From your code, you can get the PortletRequest object this way: 从您的代码中,您可以通过以下方式获取PortletRequest对象:
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. 首先:请在6.2 GA6上试用,目前已经使用了一个月。 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. 如果这是一个错误,则可能已经在以后的一个版本中修复了-您正在使用的版本已经快2年了。
Further, there's nothing that I'm aware of in the specification that allows access to the underlying HttpServletRequest
. 此外,在规范中,我没有任何东西可以访问底层的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. 我建议检查通过调试器获得的对象,并检查它们是否围绕原始servlet容器的请求或实际容器的对象进行包装。 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) 另外,比较这些请求是定向到门户网站(检查URL)还是定向到portlet本身-因为它们是在不同的上下文中部署的,所以它们将具有不同的会话(或者一个可能具有会话,而另一个可能没有会话)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.