简体   繁体   中英

Share Session between liferay portlet and servlet

I'm trying to share a session between a liferay portlet and a servlet, running in the same WAR.

I'm setting the attribute like this in the LoginPostAction (Hook):

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

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

}

Trying to get this Gebruiker object in my servlet, through an AJAX-request:

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

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

But here my 'Gebruiker-object' stays null.

What am I doing wrong?

thx

Easy: The LoginPostAction is handled by Liferay (even though technically implemented in your webapp's context/classloader. However, if you look at the httpServletRequest's context path, it's Liferay's.

When you implement a servlet in your own webapp, it will have its own session, unrelated to Liferay's.

You should rather implement a portlet and utilize its serveResource lifecycle method to handle the Ajax request - this will make you part of the whole portal environment. However, you should also minimize use of the Http-level session: It's prone to become a source for memory leaks sooner or later.

Note: While implementing a portlet will give you access to the HttpServletRequest (through PortalUtil), this is discouraged for the reasons given above. But as I don't know what you're trying to achieve, this would be part of the quickfix for the code that you give in your question.

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