掲示板

Adding Cookies with a Portlet

thumbnail
12年前 に Chris Jurado によって更新されました。

Adding Cookies with a Portlet

Junior Member 投稿: 66 参加年月日: 10/07/16 最新の投稿
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:

LiferayFacesContext liferayFacesContext = LiferayFacesContext.getInstance();
PortletResponse portletResponse = (PortletResponse) liferayFacesContext.getExternalContext().getResponse();
HttpServletResponse response = PortalUtil.getHttpServletResponse(portletResponse);

Cookie cookieName = new Cookie("USER_NAME", "Bugs Bunny");
cookieName.setDomain(".domain.com");
cookieName.setSecure(false);
cookieName.setMaxAge(3600);
cookieName.setPath("/");
response.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.
thumbnail
12年前 に Neil Griffin によって更新されました。

RE: Adding Cookies with a Portlet

Liferay Legend 投稿: 2655 参加年月日: 05/07/27 最新の投稿
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 PortletResponse.addProperty(javax.servlet.http.Cookie).

Neil
thumbnail
12年前 に Chris Jurado によって更新されました。

RE: Adding Cookies with a Portlet

Junior Member 投稿: 66 参加年月日: 10/07/16 最新の投稿
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.
thumbnail
11年前 に Bijan Vakili によって更新されました。

RE: Adding Cookies with a Portlet

Expert 投稿: 375 参加年月日: 09/03/10 最新の投稿
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 .