You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jetspeed-user@portals.apache.org by Enrique Perez <pe...@dit.upm.es> on 2007/04/09 19:29:16 UTC

How can I relate a session attribute to a portlet instance in a JSP??

Hi,

I'm trying to make available an attribute only for a group of JSPs, but 
I don't know how to tell the JSP to get the attribute from the session 
only if it's the proper portlet instance...

I think I've explained my problem poorly... =S Let's see it with an 
example...

I've made a portlet that sets a message in a session attribute (with 
application scope) so the JSP is able to retrieve it. The problem is 
that, using application scope, if there are two instances of the same 
portlet class, both are writing the same attribute, and both JSPs are 
reading the same attribute as well (when they should be reading the 
attribute only if it was the portlet instance the JSP is related to the 
one that wrote the message).

Once the portlet scope is discarded, I think I can do it by using an 
additional attribute that acts as a lock and contains the portlet ID in 
the portal context. That way, the portlet sets two attributes (one 
containing its ID and the other one containing the message) and the JSP 
gets the message only if the portlet ID is equal to the portlet instance 
it's related to.

My questions are:

1) Can I retrieve the ID the portal has assigned to the portlet instance 
from within the portlet class??
I couldn't find any method in the portlet API that fits... maybe 
*getPortletName* 
[http://www.bluesunrise.com/portlet-api/javax/portlet/PortletConfig.html#getPortletName()], 
but I think this method will return in every instance the same value =/

2) Is it possible to let the JSP know the ID of the portlet instance 
that dispatched the request to it??


Does anyone know a better solution to my problem??

Any help would be very much appreciated =)
Thanks in advance and regards,
Enrique


PS: sorry for my English =S



---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-user-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-user-help@portals.apache.org


Re: How can I relate a session attribute to a portlet instance in a JSP??

Posted by Enrique Perez <pe...@dit.upm.es>.
Ok, it was my mistake... =S

Instead of retrieving the attribute from the HttpSession in the JSP like 
this:

    <% String error_msg = (String) session.getAttribute("ERROR_MSG"); %>

it's better to retrieve it from the PortletSession like this:

    <% PortletSession session_plt = renderRequest.getPortletSession(); %>
    <% String session_attr = (String)
    session_plt.getAttribute("ERROR_MSG"); %>

so the portlet scope can be used =)

Thanks for the help!!!

Regards,
Enrique


Enrique Perez escribió:
> Hi Michael,
>
> I thought so because JSPs are all rendered in the same portlet window, 
> and I thought that was PortletScope for... But when I tried the 
> Portlet Scope, it didn't work.
>
> Anyway, I think that behavior matches to portlet specification 
> (PLT.15.4 Relationship with the Web Application HttpSession):
>
>    <quote>
>
>        The PortletSession must store all attributes in the HttpSession
>        of the portlet
>        application. A direct consequence of this is that data stored in
>        the HttpSession by
>        servlets or JSPs is accessible to portlets through the
>        PortletSession in the portlet
>        application scope. Conversely, data stored by portlets in the
>        PortletSession in the
>        *portlet application scope* is accessible to servlets and JSPs
>        through the HttpSession.
>
>    </quote>
>
> I will try again in case it works...
>
> Regards,
> Enrique
>
>
> Michael Binette escribió:
>> Enrique,
>>
>> Wouldn't using PortletScope instead of ApplicationScope work?
>>
>> -- 
>> Michael Binette
>>
>> Enrique Perez wrote:
>>> Hi,
>>>
>>> I'm trying to make available an attribute only for a group of JSPs, 
>>> but I don't know how to tell the JSP to get the attribute from the 
>>> session only if it's the proper portlet instance...
>>>
>>> I think I've explained my problem poorly... =S Let's see it with an 
>>> example...
>>>
>>> I've made a portlet that sets a message in a session attribute (with 
>>> application scope) so the JSP is able to retrieve it. The problem is 
>>> that, using application scope, if there are two instances of the 
>>> same portlet class, both are writing the same attribute, and both 
>>> JSPs are reading the same attribute as well (when they should be 
>>> reading the attribute only if it was the portlet instance the JSP is 
>>> related to the one that wrote the message).
>>>
>>> Once the portlet scope is discarded, I think I can do it by using an 
>>> additional attribute that acts as a lock and contains the portlet ID 
>>> in the portal context. That way, the portlet sets two attributes 
>>> (one containing its ID and the other one containing the message) and 
>>> the JSP gets the message only if the portlet ID is equal to the 
>>> portlet instance it's related to.
>>>
>>> My questions are:
>>>
>>> 1) Can I retrieve the ID the portal has assigned to the portlet 
>>> instance from within the portlet class??
>>> I couldn't find any method in the portlet API that fits... maybe 
>>> *getPortletName* 
>>> [http://www.bluesunrise.com/portlet-api/javax/portlet/PortletConfig.html#getPortletName()], 
>>> but I think this method will return in every instance the same value =/
>>>
>>> 2) Is it possible to let the JSP know the ID of the portlet instance 
>>> that dispatched the request to it??
>>>
>>>
>>> Does anyone know a better solution to my problem??
>>>
>>> Any help would be very much appreciated =)
>>> Thanks in advance and regards,
>>> Enrique
>>>
>>>
>>> PS: sorry for my English =S
>>>
>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: jetspeed-user-unsubscribe@portals.apache.org
>>> For additional commands, e-mail: jetspeed-user-help@portals.apache.org
>>>
>>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: jetspeed-user-unsubscribe@portals.apache.org
>> For additional commands, e-mail: jetspeed-user-help@portals.apache.org
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jetspeed-user-unsubscribe@portals.apache.org
> For additional commands, e-mail: jetspeed-user-help@portals.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-user-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-user-help@portals.apache.org


Re: How can I relate a session attribute to a portlet instance in a JSP??

Posted by Enrique Perez <pe...@dit.upm.es>.
Hi Michael,

I thought so because JSPs are all rendered in the same portlet window, 
and I thought that was PortletScope for... But when I tried the Portlet 
Scope, it didn't work.

Anyway, I think that behavior matches to portlet specification (PLT.15.4 
Relationship with the Web Application HttpSession):

    <quote>

        The PortletSession must store all attributes in the HttpSession
        of the portlet
        application. A direct consequence of this is that data stored in
        the HttpSession by
        servlets or JSPs is accessible to portlets through the
        PortletSession in the portlet
        application scope. Conversely, data stored by portlets in the
        PortletSession in the
        *portlet application scope* is accessible to servlets and JSPs
        through the HttpSession.

    </quote>

I will try again in case it works...

Regards,
Enrique


Michael Binette escribió:
> Enrique,
>
> Wouldn't using PortletScope instead of ApplicationScope work?
>
> -- 
> Michael Binette
>
> Enrique Perez wrote:
>> Hi,
>>
>> I'm trying to make available an attribute only for a group of JSPs, 
>> but I don't know how to tell the JSP to get the attribute from the 
>> session only if it's the proper portlet instance...
>>
>> I think I've explained my problem poorly... =S Let's see it with an 
>> example...
>>
>> I've made a portlet that sets a message in a session attribute (with 
>> application scope) so the JSP is able to retrieve it. The problem is 
>> that, using application scope, if there are two instances of the same 
>> portlet class, both are writing the same attribute, and both JSPs are 
>> reading the same attribute as well (when they should be reading the 
>> attribute only if it was the portlet instance the JSP is related to 
>> the one that wrote the message).
>>
>> Once the portlet scope is discarded, I think I can do it by using an 
>> additional attribute that acts as a lock and contains the portlet ID 
>> in the portal context. That way, the portlet sets two attributes (one 
>> containing its ID and the other one containing the message) and the 
>> JSP gets the message only if the portlet ID is equal to the portlet 
>> instance it's related to.
>>
>> My questions are:
>>
>> 1) Can I retrieve the ID the portal has assigned to the portlet 
>> instance from within the portlet class??
>> I couldn't find any method in the portlet API that fits... maybe 
>> *getPortletName* 
>> [http://www.bluesunrise.com/portlet-api/javax/portlet/PortletConfig.html#getPortletName()], 
>> but I think this method will return in every instance the same value =/
>>
>> 2) Is it possible to let the JSP know the ID of the portlet instance 
>> that dispatched the request to it??
>>
>>
>> Does anyone know a better solution to my problem??
>>
>> Any help would be very much appreciated =)
>> Thanks in advance and regards,
>> Enrique
>>
>>
>> PS: sorry for my English =S
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: jetspeed-user-unsubscribe@portals.apache.org
>> For additional commands, e-mail: jetspeed-user-help@portals.apache.org
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jetspeed-user-unsubscribe@portals.apache.org
> For additional commands, e-mail: jetspeed-user-help@portals.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-user-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-user-help@portals.apache.org


Re: How can I relate a session attribute to a portlet instance in a JSP??

Posted by Michael Binette <je...@adinquiry.com>.
Enrique,

Wouldn't using PortletScope instead of ApplicationScope work?

--
Michael Binette

Enrique Perez wrote:
> Hi,
>
> I'm trying to make available an attribute only for a group of JSPs, 
> but I don't know how to tell the JSP to get the attribute from the 
> session only if it's the proper portlet instance...
>
> I think I've explained my problem poorly... =S Let's see it with an 
> example...
>
> I've made a portlet that sets a message in a session attribute (with 
> application scope) so the JSP is able to retrieve it. The problem is 
> that, using application scope, if there are two instances of the same 
> portlet class, both are writing the same attribute, and both JSPs are 
> reading the same attribute as well (when they should be reading the 
> attribute only if it was the portlet instance the JSP is related to 
> the one that wrote the message).
>
> Once the portlet scope is discarded, I think I can do it by using an 
> additional attribute that acts as a lock and contains the portlet ID 
> in the portal context. That way, the portlet sets two attributes (one 
> containing its ID and the other one containing the message) and the 
> JSP gets the message only if the portlet ID is equal to the portlet 
> instance it's related to.
>
> My questions are:
>
> 1) Can I retrieve the ID the portal has assigned to the portlet 
> instance from within the portlet class??
> I couldn't find any method in the portlet API that fits... maybe 
> *getPortletName* 
> [http://www.bluesunrise.com/portlet-api/javax/portlet/PortletConfig.html#getPortletName()], 
> but I think this method will return in every instance the same value =/
>
> 2) Is it possible to let the JSP know the ID of the portlet instance 
> that dispatched the request to it??
>
>
> Does anyone know a better solution to my problem??
>
> Any help would be very much appreciated =)
> Thanks in advance and regards,
> Enrique
>
>
> PS: sorry for my English =S
>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: jetspeed-user-unsubscribe@portals.apache.org
> For additional commands, e-mail: jetspeed-user-help@portals.apache.org
>
>


---------------------------------------------------------------------
To unsubscribe, e-mail: jetspeed-user-unsubscribe@portals.apache.org
For additional commands, e-mail: jetspeed-user-help@portals.apache.org