Note that there are some explanatory texts on larger screens.

plurals
  1. POJSF 2.0: cookies are not persisted immediately after AJAX call, HTTP request required
    text
    copied!<p>Ajax call performed in order to remove item from shopping cart - <code>removeOrder()</code> method is called</p> <p>UI <code>removeOrder()</code> call(JSF&amp;Primefaces):</p> <pre><code>&lt;p:commandButton value="clean" actionListener="#{showProducts.removeOrder}" process="@form" update="@form,:ccId:cCart:ccSizeId,:ccId:cCart:ccTotId" immediate="true"&gt; &lt;f:attribute name="remove" value="#{cart.name}"/&gt; &lt;/p:commandButton&gt; </code></pre> <p>Backend <code>removeOrder()</code> call(managed bean)</p> <pre><code>public void removeOrder(ActionEvent e) { String productName = (String) e.getComponent().getAttributes().get("remove"); Product p = getProductByName(productName); inCart.remove(p); persistCookies(); emptyCartNotifier(); totalRendered(); } </code></pre> <p>Here cookies is persisted,output of this method as is expected,Cookie array contains cookies with empty values,that's OK:</p> <pre><code>private void persistCookies() { HttpServletResponse httpServletResponse = (HttpServletResponse) FacesContext.getCurrentInstance().getExternalContext().getResponse(); String ids = ""; for (Product prod : inCart) { // TODO change logic to support real count,for now 1 is available only ids += prod.getId() + ";" + prod.getCount() + "_"; } Cookie cookie = new Cookie(SC_COOKIE, ids); Cookie cookie2 = new Cookie(SC_SIZE, String.valueOf(inCart.size())); Cookie cookie3 = new Cookie(SC_TOTAL_PRICE, String.valueOf(subTotal)); cookie3.setPath("/"); cookie3.setMaxAge(TWO_WEEKS); httpServletResponse.addCookie(cookie3); cookie.setPath("/"); cookie.setMaxAge(TWO_WEEKS); cookie2.setPath("/"); cookie2.setMaxAge(TWO_WEEKS); httpServletResponse.addCookie(cookie); httpServletResponse.addCookie(cookie2); } </code></pre> <p>Here problem occurred, the method emptyCartNotifier() see <strong><em>non-empty "previous" Cookies</em></strong> array</p> <pre><code>private String emptyCartNotifier() { HttpServletRequest httpServletRequest = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); Cookie[] cookies = httpServletRequest.getCookies(); boolean isCookiePresent = false; if (cookies != null) { for (Cookie c : cookies) { if (SC_COOKIE.equals(c.getName()) &amp;&amp; (!c.getValue().isEmpty())) { isCookiePresent = true; } } } if (inCart.isEmpty() &amp;&amp; (!isCookiePresent)) { emptyCartNotifier = "you cart is empty"; isFormRendered = false; } else { emptyCartNotifier = ""; isFormRendered = true; } return emptyCartNotifier; } </code></pre> <p>After any HTTP request performed, that Cookie array is really cleaned up.</p> <p>As I see , clash is:<br>after AJAX call cleans cookie, that <code>HttpServletRequest</code> contains non-empty cookie until new HTTP request performed(user submit button or go by link).</p> <p>Is there solution or good practice for immediate cookie management,when web-app combines AJAX and non-AJAX calls?</p> <p>Thank you.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload