简体   繁体   中英

Get Portlet Session on server

My question is:
I have two different Portlets (nothing but war file) deployed in a portal server called first and second; whenever user clicks firstportlet (first) I use the following code to set session object!:

Code in first portlet:

String application="Welcome";
PortletRequest portletRequest = (PortletRequest) webAppAccess
                    .getHttpServletRequest()
                    .getAttribute(Constants.PORTLET_REQUEST);
portletRequest.getPortletSession(true).setAttribute("application",
                        sessionValue, 
                        PortletSession.APPLICATION_SCOPE);

log.Info("SESSION hole value:---" + portletRequest.getPortletSession(false));

Whenever user clicks secondportlet(second) I am using follwing code to retrieve the session which was set in firstportlet.

Code in second portlet:

PortletRequest portletRequest = (PortletRequest) webAppAccess
                    .getHttpServletRequest()
                    .getAttribute(Constants.PORTLET_REQUEST);


log.Info("SESSION hole value:---"+ portletRequest.getPortletSession(false));
log.Info("SESSION VALUE in second Portlet:----"
    + portletRequest.getPortletSession(false).getAttribute("application",
                             PortletSession.APPLICATION_SCOPE));

Error:

But in second portlet I am always getting null value, please any solution?

The portletSession.APPLICATION_SCOPE mechanism makes it possible for portlets to share session data if they are within the same portlet application. If you have two portlets that are not in the same war they are not in the same portlet application and thus, the session data is not shared either.

To solve this you need to put the two portlets into the same portlet application (same war , same portlet.xml but still two different portlets).

DanielBarbarian's answer is correct. To share the global session, the portlets need to be in the same war. But there are other ways to share data between portlets on the same page, but not necessarily in the same war. (I'm assuming your portal container supports the JSR286 standard)

  • Use interportlet communication. This can notify other portlets of events. It is a simple publish/subscribe system that is not difficult to set up.
  • Use global render parameters.

There are examples on the web for both options.

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