Access Portlet Session as Anonymous User In WebSphere Portal

By default, WebSphere Portal does not enable session tracking for anonymous users, hence the following request.getPortletSession() code snippet will always return null for anonymous users:

private static SideNavigationPortletSessionBean getSessionBean(PortletRequest request) {
 PortletSession session = request.getPortletSession();
 if (session == null)
 return null; // <-- will always return null for anonymous users
 SideNavigationPortletSessionBean sessionBean = (SideNavigationPortletSessionBean) session.getAttribute(SESSION_BEAN);
 if (sessionBean == null) {
  sessionBean = new SideNavigationPortletSessionBean();
  session.setAttribute(SESSION_BEAN, sessionBean);
 }
 return sessionBean;
}

And this set the team wondering on how out of the box IBM portlets like Sitemap portlet (PA_SearchSitemapPort.ear) transports information to the presentation layers (JSP)? Hence we de-compiled the sitemap.jar and found out that they are transporting the information through the render parameters which is something that matches the paragraph found in Accessing the portlet session on the anonymous page:

If you need to enable session tracking across requests for non-authenticated users, you can do so by setting the public.session parameter in the portal Navigator service configuration or by setting the com.ibm.portal.public.session container run time option in a JSR 286 portlet deployment descriptor. Note that this may result in significantly increased memory consumption. … Instead of using these options, portlets that need to maintain interaction state even for non-authenticated users should use render parameters to keep this information instead of the portlet session, as recommended by the Java Portlet Specification.

 

Below is an example on how to pass information using render parameters?

  1. Under doView method, set the information to RenderRequest’s parameters
    public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
     // ...
    
     request.setAttribute("rootPage", rootPage);
    
     // Invoke the JSP to render
     PortletRequestDispatcher rd = getPortletContext().getRequestDispatcher(JSP_FOLDER + VIEW_JSP);
     rd.include(request, response);
    }
  2. At JSP, retrieve the information as follows:
    <jsp:useBean id="rootElement" class="sg.xxx.xxx.sidenavigation.model.PageElement" scope="request"/>
    <%
     PageElement rootPage = (PageElement)request.getAttribute("rootPage");
     if (rootPage != null) {
      // logic ...
     }
    %>
    

Leave a Reply

Your email address will not be published. Required fields are marked *