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")

Related Searches:

References

Resources

Comments

You May Also Like

  • How to Use Response AppendHeader

    The AppendHeader method of the Response class sends custom settings to the server. These settings are used to control the browser. For...

  • How to View Response Headers

    A form of information transfer over the Internet, hypertext transfer protocol (HTTP) determines the format, method and rules in which Web browsers...

  • How to Use the Doctype Tag in XML

    XML allows users to create structure in a one-dimensional file. The file needs to be error free so any browser or other...

  • How to Set HTTP Headers

    Hypertext Transfer Protocol, or HTTP, is the language Web browsers and servers use to communicate over the Internet. Every time a browser...

  • How to Open a Word Document From a Servlet Response

    Java servlets are Web services run on host servers that provide an interface for developers to work with the internal business objects....

Related Ads

Featured