[Java] More about cookies in Vignette Application Portal (VAP)
Another blog post about cookies in VAP...
Other ways of setting cookies from a portlet?
I got this message from Sascha about setting cookies from a VAP portlet:
Hi, I've read you log about setting cookie in a portlet. First, I thought so too, but there actually _is_ a way to set a cookie from a VAP Portlet: you have to get the original HttpServletRequest from the HttpServletRequestWrapper instance in your JSP:
public static HttpServletResponse getOriginalResponse(HttpServletResponse response) {
HttpServletResponse result = response;
if (response instanceof HttpServletResponseWrapper) {
response = (HttpServletResponse)((HttpServletResponseWrapper) response).getResponse();
result = getOriginalResponse( response );
}
return result;
}
I tried this, but from a JSP page in a JSR168 solution it doesn't seem to work. I think maybe Sascha meant a VAP specific portlet/module and not a JSR168 portlet. As far as I understand it, the HTTP headers have already been written when the portlet starts to execute, so it's too late.
I got another obvious way of setting cookies from a portlet - do it from a client-side javascript:
Setting cookie through javascript:
<script>document.cookie =
"jscriptcookie=testvalue"; </script>This obviously works fine and you can also get cookies this way, but again, what I'm really after is a way to both get and set cookies from within a portlat class scope, say for example in the processAction() method. Below I have a way of getting a cookie from there, but setting it still seems to be impossible.
A better way of getting cookies from within a portlet class
Earlier we've had to grab cookies from a JSP page in a JSR168 portlet solution, but a friend of mine found out that VAP actually adds the "cookie" http header(s) to the PortletRequest properties collection, according to the JSR168 spec, section PLT.11.1.4. So from within a JSR168 portlet it's quite simple to get the cookies from the RenderRequest.getProperties("cookie"). Thank you Vignette, now make sure all future versions of VAP does the same please :)