I have a requirement where the value should not be cached on the server or in the browser as a cookie over domains and sessions.
So, I chose a constant redirect to the value
Servlet:
@Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String key = request.getParameter("key"); String val = request.getContentType(); if (val != null && val.length() == 0) { val = null; } String repeatText = request.getParameter("repeatText"); if (val != null && repeatText == null) { response.setStatus(301); // moved permanent response.addHeader("Location", "?repeatText=" + val); System.out.println("Write"); } else { if (repeatText != null) { response.setContentLength(repeatText.length()); response.addHeader("pragma", "no-cache"); response.addIntHeader("expires", BROWSER_CACHE_DAYS); response.getWriter().write(repeatText); System.out.println("Read and cache!"); } else { response.sendError(304); // use from cache! System.out.println("Send use from cache."); } } }
Script:
<input class="username" /> <button>Login</button> <script> jQuery.ajax('theservlet?key=username').done(function(v){jQuery('.username').val(v);}); jQuery('button').click(function(){ jQuery.ajax('theservlet?key=username',{contentType:jQuery('.username').val()}) }); </script>
Console Output:
Send use from cache.
After the actual download from the browser does not return the username entered.
Why doesn't the browser cache?

Peter Rader
source share