How to Use Response SetHeader
Response headers are objects that send servers information about the request. SetHeader in Java is a method used by developers to set configurations for content type. For instance, you can tell the server that the request is an XML. This helps validate the output and eliminate errors.
Instructions
-
-
1
Use setheaders to control the cache of the page. The cache is how long the user's browser saves the page content without making a new call to the server. Use the "Expires" setting to control the cache. This is a good setting for pages that are updated frequently. This also sets the proxy server from cache setting:
response.setHeader("Expires", "Thursday, February 15 2009 12:00:00 GMT"); -
2
Set the content type. This tells the server what kind of coding is used on the site. The following code sets the content type to HTML:
response.setContentType("text/html"); -
-
3
Prevent the browser from caching the page. The following code sets "No Cache" properties:
response.setHeader("Cache-Control","no-cache") -
4
Load or redirect to another page in a certain amount of time. This setting tells the browser to refresh the page in "x" seconds. The following code sets the refresh setting to 20 seconds:
response.setHeader("Refresh", "20") -
5
Set a cookie on the user's browser using the addCookie method. The following command sets a cookie on the user's computer for saved content and settings:
response.addCookie ("MyCookie", "myValue")
-
1