3 Replies Latest reply on Mar 9, 2009 2:44 PM by apemberton

    Accessing session attributes in a portlet

      I have a servlet filter intercepting all http reqs to my portal and setting session attributes (strings) that I'd like to access in portlets. Is this possible? If so, can someone tell me what I'm doing wrong?

      #########
      In the servlet:
      #########
      HttpSession session = httpRequest.getSession(true);
      session.setAttribute("attr1","somestring");

      #########
      In the portlet:
      #########
      String attribute = (String)readRequest.getPortletSession().getAttribute("attr1", PortletSession.APPLICATION_SCOPE);

      // Always returns null for some reason???
      System.out.println(attribute);

      Cheers,
      -Steve

        • 1. Re: Accessing session attributes in a portlet

          Steve:

          You might clarify by adding your Portal version info, etc., but I'll take a stab at an answer anyway.

          Your servlet listener will intercept requests to the underlying portal servlet, whereas the portlet code you showed there is accessing the portlet's session. These two session are inherently different (different WAR deployments)

          Instead try the following in your portlet code:

          Navigation.getPortalRuntimeContext().getSession().getAttribute("attr1");


          This will allow you to access the underlying portal servlet's global session from your portlet code.

          • 2. Re: Accessing session attributes in a portlet

            I am using Jboss portal 2.6.8. I tried the code below, but I still get null value when attempting to read "attr1".

            I set "attr1", an HttpSession attribute, in the doFilter() of my filter class. I believe that this is set before the request gets to the portal servlet.

            I'm not to JB portal, but from what I am reading, there might not be a way to read this attribute from my portlet.

            Here is a similar post with no response from '05:
            http://www.jboss.com/index.html?module=bb&op=viewtopic&p=4122211#4122211

            Thoughts?

            • 3. Re: Accessing session attributes in a portlet

              Ah, understood. See the logic in PortletTag.java from the JBoss Portal code base to see how to access the underlying HttpServletRequest.

              Something like the following will get you access to the underlying ServletRequest:

              ServletRequest req = pageContext.getRequest();
              PortletInvocation invocation = (PortletInvocation)req.getAttribute(ContextDispatcherInterceptor.REQ_ATT_COMPONENT_INVOCATION);
              HttpServletRequest request = invocation.getDispatchedRequest();