i have a requirement where a value should not be cached in the server nor the browser as an cookie over domains and sessions.
So i choosed to permanent-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.
--- i enter username and press the button ---
Write
Read and cache!
--- now i make a reload ---
Send use from cache.
After the reaload from the browsercache does not return the username i inserted.
Why does the browser not cache?