Naresh Kallamadi
Naresh Kallamadi

Reputation: 163

Liferay: How to add cookie from portlet from doEdit(RenderRequest req,RenderResponse res)

I am trying to set a cookie from portlet doEdit method like the following :

Public void doEdit(RenderRequest request, RenderResponse response)
    throws PortletException, IOException 
{
      Cookie cookie = new Cookie("url","mkyong dot com");
      cookie.setMaxAge(60*60); //1 hour
      res.addCookie(cookie);
}

Here I am getting a error like: The method addCookie(Cookie) is undefined for the type RenderResponse

So can any one help me out

Upvotes: 0

Views: 2648

Answers (3)

Vishal Shah
Vishal Shah

Reputation: 641

Try to use below code for the creating the cookie

Cookie paramCookie = new Cookie("Key", "Vishal Shah");
paramCookie.setVersion(0);
paramCookie.setMaxAge(4 * 24 * 60 * 60); //4 days
renderResponse.addProperty(paramCookie);

As per your code you miss the version setting line.

I am using this above lines of code in porcessAction(ActionRequest actionRequest, ActionResponse actionResponse) method which working properly.

Upvotes: 0

Olaf Kock
Olaf Kock

Reputation: 48067

Note: The Portlet spec has this to say about cookies:

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. Thus the portlet should not assume that it has access to the cookie on the client or that request triggered with URLs not generated by the portlet API can access the cookie.

Cookies set in the response of one lifecycle call should be available to the portlet in the subsequent lifecycle calls, e.g. setting a cookie in processAction should enable the portlet to retrieve the cookie in the next render call.

For requests triggered via portlet URLs the portlet should receive back the cookie. Cookies can be retrieved via the request.getCookies method.

Cookies are properties and all restrictions said above about properties also apply for cookies, i.e. to be successfully transmitted back to the client, cookies must be set before the response is committed. Cookies set in render or serveResource after the response is committed will be ignored by the portlet container.

When setting cookies in the render lifecycle phase portlets should set the cookies in the render headers part or simply override the GenericPortlet.doHeaders method in order to run with maximum performance on all portal implementations (see PLT.11.1.1.4.3).

Thus, if you literally depend on an HTTP Cookie to be available in the browser: You can't assume that it's there, as it's only a portal internal "property", simulating the behaviour of a cookie.

If you need a Cookie that's available on HTTP/Browser, you'll have to depend on the portal implementation (check it) or set it through Javascript, or find a different solution. If you just need the data to be available, use addProperty, just as Ankit P mentions in the other answer. I doubt though (due to the spec) that you'll need to get the HttpServletRequest for it: The cookie should be available on the PortletRequest (of the portlet originating the request) already.

Upvotes: 4

Ankit P
Ankit P

Reputation: 534

Trying setting through addProperty(.)

import com.liferay.util.CookieUtil;

Cookie cookie = new Cookie("url","mkyong dot com");
cookie.setMaxAge(60*60);
renderResponse.addProperty(cookie);

To read cookie from request:

HttpServletRequest request = PortalUtil.getHttpServletRequest(
                portletRequest);

url = CookieUtil.get(request, "url");

Upvotes: 0

Related Questions