How to Read and Write Additional Headers
Reading request header values and setting headers for the response is straight-forward:
- Use getHeader to read a request header value.
- getIntHeader and getDateHeader are convenience methods that convert the value to int or a date.
- getHeaderNames returns an Enumeration of all headers in the request.
- Use setHeader to set a response header value (possibly overwriting a previous value).
- setIntHeader and setDateHeader are convenience methods for int and date values.
- Use addHeader to add a response header value (if there already was a value before, you add a second header).
- addIntHeader and addDateHeader are convenience methods for int and date values.
- containsHeader allows you to find out whether there is already a header of the same name.
This example uses the User-Agent header to detect whether the browser supports Google Chrome Frame and if it does, enables it:
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException {
String ua = request.getHeader("user-agent"); // name is case-insensitive
if (ua != null && ua.contains("chromeframe")) // detect google chrome frame (more)
response.addHeader("X-UA-Compatible", "chrome=1"); // add header (more)
// ...
}

