Adding new cookie from controller command - IBM WebSphere Commerce
In a previous post we have seen how to fetch a cookie value in controller command. Here in this post we will see how to add a new cookie from a controller command.
For fetching the cookie value in controller command we first obtained the http servlet request object from the command context. But in order to add a new cookie we need to get the http servlet response object.
Below is sample code to get the http servlet response object from command context, create a new cookie and to add the cookie to the response.
Hope this help someone. If you find any issue in above code or know better alternatives to this code, do share with us through the comment section. Feel free to share this page with your friends and colleagues.
Thanks!
For fetching the cookie value in controller command we first obtained the http servlet request object from the command context. But in order to add a new cookie we need to get the http servlet response object.
Below is sample code to get the http servlet response object from command context, create a new cookie and to add the cookie to the response.
RuntimeHttpServletResponseWrapper httpServletRes =
(RuntimeHttpServletResponseWrapper) ((CacheProxyResponse) (((ViewCommandContext) getCommandContext()).getResponse()))
.getResponse();
Cookie cookie = new Cookie("MyCookie", "MyCookieValue");
//optionally you may use these methods to set other parameters as required
//cookie.setDomain() //cookie.setMaxAge() //cookie.setPath()
httpServletRes.addCookie(cookie);
Hope this help someone. If you find any issue in above code or know better alternatives to this code, do share with us through the comment section. Feel free to share this page with your friends and colleagues.
Thanks!
Comments
Post a Comment