A servlet can set headers of an HTTP response via the following methods of the HttpServletResponse interface:
The setHeader method sets a header with a given name and value. If a previous header exists, it is replaced by the new header. In the case where a set of header values exist for the given name , all values are cleared and replaced with the new value. The addHeader method adds a header value to the set of headers with a given name. If there are no headers already associated with the given name, this method will create a new set. Headers may contain data that is better expressed as an int or a Date object. The following convenience methods of the HttpServletResponse interface allow a servlet to set a header using the correct formatting for the appropriate data type:
In order to be successfully transmitted back to the client, headers must be set before the response is committed. Any headers set after the response is committed will be ignored by the servlet container. |