Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Chris Jurado
Adding Cookies with a Portlet
May 11, 2012 9:01 AM
Answer

Chris Jurado

Rank: Junior Member

Posts: 40

Join Date: July 15, 2010

Recent Posts

Hey all,

So I'm attempting to store cookies from within a portlet to be read by a separate application (same domain).

Here's the code that creates and adds the cookie:

 1LiferayFacesContext liferayFacesContext = LiferayFacesContext.getInstance();
 2PortletResponse portletResponse = (PortletResponse) liferayFacesContext.getExternalContext().getResponse();
 3HttpServletResponse response = PortalUtil.getHttpServletResponse(portletResponse);
 4
 5Cookie cookieName = new Cookie("USER_NAME", "Bugs Bunny");
 6cookieName.setDomain(".domain.com");
 7cookieName.setSecure(false);
 8cookieName.setMaxAge(3600);
 9cookieName.setPath("/");
10response.addCookie(cookieName);


But the cookie doesn't seem to be making it. I'm watching in the browser (Firefox Page Info) as well as a little portlet I write that displays cookies, attributes, etc. and it's not there. No error messages... just no cookie.

Note: This is using PortletFacesbrige 2.0.0, and not the latest 3.0, in accordance with our other portlet projects. Is it possible that upgrading to the LiferayFaces release will clear this up, or am I just doing something wrong?

Thanks.
Neil Griffin
RE: Adding Cookies with a Portlet
May 11, 2012 9:21 AM
Answer

Neil Griffin

LIFERAY STAFF

Rank: Liferay Legend

Posts: 2235

Join Date: July 26, 2005

Recent Posts

Hi Chris,

Upgrading in general is a good thing, but I don't think there are any changes in Liferay Faces Bridge that would help with this one. Instead I would recommend trying [url=http://portals.apache.org/pluto/portlet-2.0-apidocs/javax/portlet/PortletResponse.html#addProperty(javax.servlet.http.Cookie)]PortletResponse.addProperty(javax.servlet.http.Cookie).

Neil
Chris Jurado
RE: Adding Cookies with a Portlet
May 11, 2012 9:57 AM
Answer

Chris Jurado

Rank: Junior Member

Posts: 40

Join Date: July 15, 2010

Recent Posts

Hey Neil,

Thanks for the suggestion. I had actually tried that approach as well, because it seems clunky to have to get the HttpServletResponse object when you already have the PortletResponse object..

In order to test this I set the hosts file in my local development box to associate the domain I'm using with the cookie to the home IP address. Part of me wonders if that isn't related somehow.
Bijan Vakili
RE: Adding Cookies with a Portlet
September 28, 2012 6:22 AM
Answer

Bijan Vakili

Rank: Regular Member

Posts: 170

Join Date: March 10, 2009

Recent Posts

Hey,
Per Portlet 2.0 spec section PLT.12.1.4 Setting Cookies:
"A portlet can set HTTP cookies at the response via the addProperty method with a
javax.servlet.http.Cookie as parameter. The portal application is not required to
transfer the cookie to the client."

Also, relating to this may be the Liferay Portal version number. For example, see LPS-1728 http://issues.liferay.com/browse/LPS-1728?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel#issue-tabs .