Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • How to set a Cookie in Liferay portlet?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2.16k
    Comment on it

    Hello Guys
    Liferay is an EMS portal to develop java based web appliction and having portlets to write business logics. We can set cookies in liferay portlet without heavily modifying the Liferay portal itself, The best way to set the portlet cookies is to let the portlet generate a javascript, which will then let the client to set the cookie.

    So I developed the following to the doView method to set cookies :

    if (renderRequest.getPortletSession(true).getAttribute("set_cookie")!=null){
    String cookie_value = renderRequest.getPortletSession(true).getId();
    String cookie_hours = "6";
    StringBuffer buf = new StringBuffer();
    buf.append("\n <script>");
    buf.append("\n var today = new Date();");
    buf.append("\n var expires_date = new Date ( today.getTime() + (" + cookie_hours + "*1000*60*60) );");
    buf.append("\n document.cookie = \"linkedIn=" + util.getAppKey() + ";expires=\"+expires_date.toGMTString();");    
    buf.append("\n </script>");
    PrintWriter out = renderResponse.getWriter();
    renderRequest.getPortletSession(true).setAttribute(SET_COOKIE, cookie_value);

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: