|
Home > Archive > WebSphere Portal Server > April 2004 > Creating a cookie
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| aymanm1 2004-04-15, 7:34 pm |
| Hello,
I want my portlet to create a cookie and add it to the portlet response when it receives an ActionEvent. Here are my questions. Thanks in advance
1. Can I create the cookie only in beginPage? What if I want to create it in actionPerformed() OR doView() methods?
2. If I have to create and add the cookie in beginPage, then how can I do that in response to an action that is being processed in actionPerformed method?
3. If someone could provide a small example, I would really appreciate it.
Thanks
AME
| |
| Michael Harris 2004-04-18, 10:39 am |
| Cookies created with the Java API can only be created in the beginPage
method. An exception is thrown if addCookie() is called in any other
context.
The issue with cookies and beginPage is that cookies must be transmitted to
the client in the HTTP header before the markup stream. Therefore, you must
set them early enough in the page processing such that the buffers haven't
filled up and the markup starts flowing across the TCP connection to the
client.
I haven't tried this but, to create a cookie as a result of an action event,
you can detect the condition in actionPerformed() and then use an object in
the PortletRequest to signal the beginPage() code to create the cookie.
actionPerformed() is always called prior to any method call that outputs
markup.
For the doView() case, beginPage() has already been called and therefore you
have no way via the Java API to create a cookie. Your only choice at this
point is to use client-side scripting to create the cookie. If your client
is a web browser that has JavaScript enabled, you can use that. If your
client is a WAP phone or PockePC or other device, you're pretty much out of
luck.
|
|
|
|
|