Creating HTTP Headers
An
rwsf::HttpServletResponse contains a collection of headers that the servlet container uses for the response to the client. The response provides the function
setHeader() for adding free-form text headers, the function
setDateHeader() for creating headers that contain a date, and the function
setIntHeader() for creating headers that contain a number. The response also provides named accessors for the
Content‑Type and
Content-Length headers.
The servlet container does not include a Content-Type header by default. A response must set the Content-Type to the MIME type of the content in the payload.
The following code uses a named accessor to set the Content-Type of a response to text/xml:
response.setContentType("text/xml");
The code sample below adds a nonstandard HTTP header to a response:
response.setHeader("X-Servlet-Name", "myServlet");
The response provides a
setDateHeader function for creating a header from an
rwsf::DateTime object. The code sample below uses the
setDateHeader() function to create an
Expires HTTP header with a time two days from the present. The sample below creates an
rwsf::DateTime with the current time, adds 48 hours to the time, converts the time to HTTP date format, then adds the header to the request:
rwsf::DateTime expires(rwsf::DateTime::setCurrentTime);
expires.incrementHour(48);
response.setDateHeader("Expires", expires);