BHSM-Servlet does not allow browser cache username

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. --- i enter username and press the button --- Write Read and cache! --- now i make a reload --- Send use from cache. 

After the actual download from the browser does not return the username entered.

Why doesn't the browser cache?

EDIT: Added for explaintaition

+7
java jquery servlets
source share
3 answers

Your request is an ajax call, which is the async request for your servlet, in which case your redirection on the servers does not affect the clients. This request works as a separate thread in your browser.

So, you must specify the response data (url in this case with the query string). After the servlet responds in your ajax request callback, you must specify the location to forward the event to.

To redirect after success ajax window.location.href = reponseUrl; instead of redirecting, you can set the value of your input.

Otherwise, just submit the form and redirect through your servlet.

0
source share

Redirecting an ajax request does not redirect the browser to a new location, and if that happened you would no longer have your page, you would just get a response from the servlet.

So, when you refresh the page, you again request the original URL and lose the username.

You can add username to url directly in javascript:

 jQuery('button').click(function(){ var username=jQuery('.username').val(); jQuery.ajax('theservlet?key=username',{contentType:username}); window.location.hash=username; }); 

This will add "#username" to the URL in the address bar. And then when loading the page, you can fill in the input from the request parameter, if any:

 jQuery('.username').val( window.location.hash ); 
0
source share

Even if you earn mechanism 301, you won’t have any guarantees as to how long the browser will cache redirects. I don’t have an answer right away why 301s are not cached for you, however, if you are ok with the browser where the username is stored somewhere (after all, this is what you are trying to do with 301) and it’s just that You specifically do not want it to be stored in a cookie, then the correct choice is sessionStorage or localStorage :

 sessionStorage.setItem("username", username); 

and

 var username = sessionStorage.getItem("username"); 

or use localStorage to persist it in sessions.

Docs here

0
source share

All Articles