[ Team LiB ] Previous Section Next Section

Common Response Headers

If you look all the way back to Figure 4.1, you'll see a typical response sent by a Web server. There are many variations of response headers, but relatively few that you need to worry about when you write Java Web applications.

The Content-Type Header

The Content-Type header is the most important response header. It tells the browser how to interpret the data it receives. A JSP has a default content type of text/html, although you can use a JSP page directive to change the content type. If you are returning XML data, for instance, you use a content type of text/xml.

The Content-Length Header

The Content-Length header is important for many types of content that contain binary data. It tells the browser exactly how many bytes are in the body of the response. That way the browser can read the full response without worrying about whether it has received everything.

The Cache-Control Header

The Cache-Control header allows you to control how long the browser keeps a particular page cached. As you saw in Figure 4.1, a JSP normally isn't cached at all. You can request that a page be cached for 5 minutes (300 seconds) by using the following response header:


Cache-Control: maxage=300

Should I Remember You?

graphics/bytheway_icon.gif

The Cache-Control header controls only whether the browser uses the cache the next time it loads a page; it does not force the browser to reload the page when it expires. To reload a page after a specific period of time, include the tag


<META HTTP-EQUIV="Refresh" Content = "30; URL=YourJSPUrlHere.jsp">

in your JSP or HTML file. The 30 in the tag is the number of seconds to wait before refreshing.


    [ Team LiB ] Previous Section Next Section