2015-02-06 32 views
1

我试图在同一个WAR中运行的liferay portlet和servlet之间共享一个会话。分享liferay portlet和servlet之间的会话

我设置这样的属性在LoginPostAction(钩):

@Override 
public void run(HttpServletRequest request, HttpServletResponse response) throws ActionException { 

    Gebruiker g = new Gebruiker(); 
    request.getSession().setAttribute("gebruiker", gebruiker); 

} 

试图让这个Gebruiker对象在我的servlet,通过AJAX请求:

@RequestMapping(value="/haalContactGegevens", method = RequestMethod.POST) 
public @ResponseBody ContactGegevensMessage getContactGegevens(HttpServletRequest request, HttpServletResponse response) { 

    Gebruiker gebruiker = (Gebruiker)request.getSession(true).getAttribute("gebruiker"); 
} 

但这里我的'Gebruiker-object'保持为空。

我在做什么错?

THX

回答

2

简单:该LoginPostAction由Liferay的(即使在你的web应用的上下文/类加载器在技术上实现的处理不过,如果你看的HttpServletRequest的上下文路径,这是Liferay的

当你实现在您自己的Web应用程序中使用servlet,它将拥有自己的会话,与Liferay无关。

您应该实施portlet并利用其serveResource生命周期方法来处理Ajax请求 - 这将使您成为整个门户环境的一部分但是,你守ld也尽量减少Http级别会话的使用:它很可能迟早会成为内存泄漏的来源。

注意:虽然实现一个portlet将使您可以访问HttpServletRequest(通过PortalUtil),但由于上述原因,这是不鼓励的。但是,因为我不知道你想要达到什么目的,所以这将成为你在问题中给出的代码的quickfix的一部分。