mahesh
mahesh

Reputation: 969

Liferay: How to add cookie from portlet

I trying to add cookie from portlet using

HttpServletResponse response=PortalUtil.getHttpServletResponse(renderResponse);
Cookie cookie=new Cookie("abc","123");
response.addCookie(cookie);

But when i am trying to print value for this cookie i am not able to find this cookie. Can anyone suggest me the solution for this?

Upvotes: 0

Views: 4431

Answers (1)

Daniele Baggio
Daniele Baggio

Reputation: 2257

Not so easy discover how to use a cookie in your portlet,

This cose sample is ok and is written within the portlet render code. Use the RenderResponse, not the HttpServletResponse.

import com.liferay.util.CookieUtil;

Cookie cookie = new Cookie("name", value);

renderResponse.addProperty(cookie);

And to read the cookie from the request:

HttpServletRequest request = PortalUtil.getHttpServletRequest(
                portletRequest);

orderCartNumber = CookieUtil.get(request, "name");

bye

Upvotes: 1

Related Questions