5 Replies Latest reply on Oct 28, 2005 5:00 AM by cpage cpage

    HttpSession and PortletSession

    cpage cpage Novice

      Hi,

      work this JBP 2.0

      main question : how can I exchange informations between the portal and a portlet ?

      =>
      I try to do an Hello User Portlet: an user connect to the portal and the portlet say Hello + UserName

      to do so, i have this code in my class and it works well :

      public class HelloUser extends GenericPortlet {
      
       public void doView(RenderRequest req, RenderResponse res) throws PortletException, IOException {
       res.setContentType("text/html");
       try {
       PortletSession session = req.getPortletSession();
       PrintWriter writer = res.getWriter();
      
       Map ua = (Map) req.getAttribute( RenderRequest.USER_INFO );
       if( ua != null ) {
       writer.write( "Bienvenue à l'utilisateur"+req.getRemoteUser() + " !!<br>" );
      
       }else{
       writer.write("Aucun Utilisateur connecté");
       }
       writer.close();
      
       }
       catch (IOException e)
       {
       e.printStackTrace();
       }
       }
      
      }


      Second,

      I try to get an Attribute in my hello user portlet. This attribute is set in the login.jsp, this way :

      Login.jsp:
      ...
      <%
      HttpSession Myses = request.getSession();
      MySes.setAttribute("toto","youhou !!");
      /%>
      ...

      I verified, the attributes "toto" is setting to "Youhou !!"


      but, i just cant' get the attribut "toto" in my portlet:

      my code:
      helloUser.java
      PortletSession session = req.getPortletSession();
       String mytoto = (String)session.getAttribute("toto",PortletSession.APPLICATION_SCOPE );


      it returns "null".


      can you help me, please ?

      thanks
      Lionel

        • 1. Re: HttpSession and PortletSession
          Bradley Smith Master

          Try changing your login.jsp in the following ways:

          1.) Add the Portlets tag library:

          <%@ taglib prefix="portlet" uri="http://java.sun.com/portlet" %>


          2.) Use <portlet:defineObjects/> tag

          3.) Change your session code to use the PortletSession instead of the HttpSession

          Ultimately, your page should look something like this:

          ...
          <%@ taglib prefix="portlet" uri="http://java.sun.com/portlet" %>
          ...
          <portlet:defineObjects/>
          ...
          ...
          <% portletRequest.getSession.setAttribute("toto","youhou!!"); %>
          ....
          


          BTW - in JSP, the HttpSession is an implicit object so you can access if simply using session instead of request.getSession().

          Hope this helps!


          • 2. Re: HttpSession and PortletSession
            Bradley Smith Master

            Ooops - code should read:

            renderRequest.getSession(...)


            NOT


            portletRequest.getSession(...)

            • 3. Re: HttpSession and PortletSession
              Bradley Smith Master

              OOPs one more time - should be

              getPortletSession()

              not getSession()

              • 4. Re: HttpSession and PortletSession
                Viet Master

                it is not possible because the sessions are not the same.

                one thing you can do is put a jsp in your webapp containing your portlet and request dispatch to it with a cross context call similar to :

                req.setAttribute("blah", "blah");
                RequestDispatcher rd = getServletContext().getContext("/myapp").getRequestDispatcher("/my.jsp");
                rd.include(req, resp);
                


                and in the jsp

                String s = (String)req.getAttribute("blah");
                HttpSession ses = req.getSession();
                ses.setAttribute("blah", "blah");
                



                • 5. Re: HttpSession and PortletSession
                  cpage cpage Novice

                   

                  "julien@jboss.com" wrote:
                  it is not possible because the sessions are not the same.


                  => impossible to a portlet to get informations put in the httpsession of the portal ? so, what is the mecanism of that code in my portlet req.getAttribute(RenderRequest.USER_INFO ) ?


                  "julien@jboss.com" wrote:

                  one thing you can do is put a jsp in your webapp containing your portlet and request dispatch to it with a cross context call similar to :

                  req.setAttribute("blah", "blah");
                  RequestDispatcher rd = getServletContext().getContext("/myapp").getRequestDispatcher("/my.jsp");
                  rd.include(req, resp);
                  


                  and in the jsp

                  String s = (String)req.getAttribute("blah");
                  HttpSession ses = req.getSession();
                  ses.setAttribute("blah", "blah");
                  



                  don't understand what you mean.

                  i just want to share a context (ie: user role store in an other database than portal DB).
                  this role (may be just a string), is known by the portal at the connection of the user and have to be get by all the portlets in the page.

                  the portlets, with this role, ask informations to the role database to display their content.


                  how can i give informations from the portal to the portlets simply ?

                  thanks a lot ;)

                  Lionel