Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set header in HttpServletResponse and Html <head> tag is the same?

Tags:

java

In HttpServletResponse, invocation of setHeader() method is the same as we set value in html <head> tag?

Java Code:

response.setHeader("Pragma", "no-cache");

response.setIntHeader("Expires", -1);

Html:

<HTML><HEAD>
<META HTTP-EQUIV="Pragma" CONTENT="no-cache">
<META HTTP-EQUIV="Expires" CONTENT="-1">
</HEAD><BODY>
</BODY>
</HTML>

Are both of them the same?

like image 325
Barcelona Avatar asked Jun 10 '11 09:06

Barcelona


1 Answers

No.

Unlike META tags in HTML documents, HTTP response headers set by the HttpServletResponse methods can be interpreted and acted upon by any of the nodes in the network, that exist between the client and the server. Typically these are proxies, which do not bother with the content of the HTML documents. The reason such elements can act on these headers, is that the HTTP response headers apply not only to the client(browser) but also to any of the intermediaries; the HTTP specification allows for interpretation of headers by intermediaries.

If you want all elements in a network to obey the cache headers, which you are intending to set (in the HTML document), then specify the HTTP response headers (using the HttpServletResponse object). All intermediaries are expected to obey these headers.

Only browsers tend to act on the META tags, for only they parse the HTML document; in other words, the META tags will determine the expiry settings of a document for the browser local cache, and not of the proxy caches.

Related resources

  1. Caching Tutorial for Web Authors and Webmasters. Refer the section on HTML Meta Tags vs. HTTP Headers.
  2. Caching in HTTP. Straight from the HTTP 1.1 specification. The behavior of the client, origin server and intermediaries is specified here.
  3. Meta data in the HTML 4.01 Specification. The HTML specification, quite obviously states that HTTP servers may use the property name specified by the http-equiv attribute to create an [RFC822]-style header in the HTTP response. Servers do not seem to adhere to this, since the word 'may' indicates that this is an optional feature.
like image 103
Vineet Reynolds Avatar answered Oct 06 '22 02:10

Vineet Reynolds