Skip to content

Commit b287da9

Browse files
author
shreeram09
committed
answer to back button cache
1 parent de7fd1f commit b287da9

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

jsp-questions.md

+27-1
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,33 @@ The jsp page, by default, always creates a session. Using a directive pagewith a
436436
## Q. What is the difference between JSPWriter and PrintWriter?
437437
`PrintWriter` is the object responsible for recording the contents of the response to the request. `JspWriter` uses an object `PrintWriter` to buffer. When the buffer is full or flushed, it `JspWriter`uses the object `PrintWriter` to write the content in response.
438438

439-
#### Q. How to disable caching on back button of the browser?
439+
## Q. How to disable caching on back button of the browser?
440+
for this, once the session is invalidated, in your respective jsp page add following code snippet
441+
```jsp
442+
<%
443+
response.setHeader("Cache-Control","no-cache, no-store, must-revalidate");
444+
response.setHeader("Pragma","no-cache");
445+
response.setHeader ("Expires", 0);
446+
response.setDateHeader ("Expires", -1);
447+
if(session.getAttribute("token")==null){
448+
response.sendRedirect("login.jsp");
449+
}
450+
%>
451+
```
452+
_`token` can be any valid session attribute used for validation_
453+
454+
**Cache-Control** : HTTP 1.1 header filed holds directives (in requests and responses) that control caching in browsers and shared chaches eg. proxies , CDNs.
455+
- no-cache : allows caches to store a response, but requires them to revalidate it before reuse.
456+
- no-store : any caches of any kind (private or shared) should not store this request and corresponding response.
457+
- must-revalidate: cache either revalidates the stored response with the origin server, or if that's not possible it generates a 504 (Gateway Timeout) response to prevent reuse of stale responses when they are disconnected from the origin server.
458+
459+
**Pragma** : HTTP 1.0 header is an implementation-specific header that may have various effects along the request-response chain to prevent the client from caching the response.
460+
- no-cache: Forces caches to submit the request to the origin server for validation before a cached copy is released.
461+
462+
**Expires**: HTTP header contains the date/time after which the response is considered expired.
463+
- Invalid expiration dates with value 0 represent a date in the past and mean that the resource is already expired.
464+
- `setDateHeader()` used in case to prevent caching on proxy servers
465+
440466
#### Q. What are the different tags provided in JSTL?
441467
#### Q. How is JSP better than Servlet technology?
442468
#### Q. What are the differences between include directive and include action?

0 commit comments

Comments
 (0)