10 Replies Latest reply on Jul 10, 2006 5:01 AM by derek_wen

    How to passing session varibles from Portlet to Servlet

    derek_wen

      Hi All,

      How to passing session varibles from Portlet to Servlet?

      thanks,
      Derek

        • 1. Re: How to passing session varibles from Portlet to Servlet
          theute

          By using the application scope session.

          • 2. Re: How to passing session varibles from Portlet to Servlet
            derek_wen

            Can you show me example?

            thanks.

            • 3. Re: How to passing session varibles from Portlet to Servlet
              theute

              You should read the spec... Section 15

              PLT.15.3 Binding Attributes into a Session
              A portlet can bind an object attribute into a PortletSession by name.
              The PortletSession interface defines two scopes for storing objects,
              APPLICATION_SCOPE and PORTLET_SCOPE.
              Any object stored in the session using the APPLICATION_SCOPE is available to any other
              portlet that belongs to the same portlet application and that handles a request identified as
              being a part of the same session.cx
              Objects stored in the session using the PORTLET_SCOPE must be available to the portlet
              during requests for the same portlet window that the objects where stored from.cxi The
              object must be stored in the APPLICATION_SCOPE with the following fabricated attribute
              name ?javax.portlet.p.<ID>?<ATTRIBUTE_NAME>?. <ID> is a unique identification for
              the portlet window (assigned by the portal/portlet-container) that must not contain a ???
              character.cxii <ATTRIBUTE_NAME> is the attribute name used to set the object in the
              PORTLET_SCOPE of the portlet session.
              Attributes stored in the PORTLET_SCOPE are not protected from other web components of
              the portlet application. They are just conveniently namespaced.
              The setAttribute method of the PortletSession interface binds an object to the
              session into the specified scope. For example:
              PortletSession session = request.getSession(true);
              URL url = new URL(?http://www.foo.com?);
              session.setAttribute(?home.url?,url,PortletSession.APPLICATION_SCOPE);
              session.setAttribute(?bkg.color?,?RED?,PortletSession.PORTLET_SCOPE);


              • 4. Re: How to passing session varibles from Portlet to Servlet
                keletappi

                 


                Attributes stored in the PORTLET_SCOPE are not protected from other web components of the portlet application.


                It says that attributes in PORTLET_SCOPE are not protected from other web components of portlet application.

                But does it really say how these attributes are accessible from Servlet.

                When I put attribute with PortletSession.setAttribute() into APPLICATION_SCOPE is it accessible with HttpSession.getAttribute() in my Servlet in same war ?


                • 5. Re: How to passing session varibles from Portlet to Servlet
                  theute

                  Yes, please read the spec !
                  All the details are in the section i mentionned and after, i cannot seriously copy and paste the whole spec

                  • 6. Re: How to passing session varibles from Portlet to Servlet
                    keletappi

                    Well. I have read the spec many times but I really don't find it neccessary to memorize everything. It is just that it hasn't been really issue for me but I know in some point I need this so I find this information useful - no need to reread it ;)

                    • 7. Re: How to passing session varibles from Portlet to Servlet
                      halversp

                      If you're trying to use a servlet to handle portlet requests, you might want to take a look at the Apache Portals Bridges project, which provides various mechanisms to address the differences between a portlet request context and the more general servlet request context. For example, there's a bridge between portlets and Apache struts.

                      pch

                      • 8. Re: How to passing session varibles from Portlet to Servlet

                         

                        "keletappi" wrote:

                        When I put attribute with PortletSession.setAttribute() into APPLICATION_SCOPE is it accessible with HttpSession.getAttribute() in my Servlet in same war ?


                        Yes.


                        • 9. Re: How to pass session varibles from Portlet to Servlet
                          lithium56

                          When using JSR 168, and you want to pass PortletSession to HttpSession, view this link:
                          http://www-03.ibm.com/developerworks/blogs/page/Joey_Bernal? entry=portlet_session_scope_and_sharing

                          The key is to put PortletSession session = request.getPortletSession();
                          in you doView method of your Portlet class (not the pagecode).

                          In your servlet, you can then :
                          public void doPost(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException {

                          HttpSession session = req.getSession(true);

                          Whatever something = (Whatever)session.getAttribute("portletObject");
                          ...
                          }
                          To make sure you have the correct portlet object, you can use the following code to make sure:


                          Enumeration attributes = session.getAttributeNames();

                          while (attributes.hasMoreElements()) {
                          System.out.println(attributes.nextElement().toString());
                          }

                          The spec just allows the implementer to use name space encoding when mapping attributes within the session. You can't simply access the portletSession with the HttpSession without the above solution as was suggested.

                          Hope that helps!!
                          Tyler

                          • 10. Re: How to passing session varibles from Portlet to Servlet
                            derek_wen

                            Hi Tyler,

                            many thanks for your help. :)

                            Derek