the managed bean :
@named @viewscoped public class bean implements serializable { private static final long serialversionuid = 1l; public bean() {} @postconstruct public void init() { cookie cookie = new cookie("token", uuid.randomuuid().tostring()); cookie.setmaxage(-1); ((httpservletresponse) facescontext.getcurrentinstance().getexternalcontext().getresponse()).addcookie(cookie); } }
the cookie named token
added response in @postconstruct
method.
this cookie attempted accessed javascript follows.
<h:head> <script type="text/javascript"> var cookie = getcookie("token"); console.log(cookie); function getcookie(name) { var matched = document.cookie.match(regexp(name + "=.[^;]*")); if (matched) { return matched[0].split('=')[1]; } return false; } </script> </h:head> <h:body> <h:form> #{bean} <h:form> </h:body>
it works fine unless same page opened simultaneously in different tabs and/or windows happens, when link corresponding page clicked several times holding ctrl key in case token shared across different tabs expected unique across tabs and/or windows.
using <f:event type="prerenderview" listener="#{bean.init}">
, <f:viewaction action="#{bean.init}">
or servlet filer set cookie little earlier makes no difference.
how make sure cookie value according new generated value in managed bean , not collide, when same page requested , opened in different tabs and/or windows?
Comments
Post a Comment