Setting the Response from a String
An rwsf::ServletResponse also provides a setPayload() function. The function sets the body of the response directly, from a single std::string. Setting the payload directly is generally more efficient than using the rwsf::ServletOutputStream class to write the response. The doGet() function below constructs a std::string, then uses the setPayload() function to create the response.
void myServlet::doGet (rwsf::HttpServletRequest& request,
rwsf::HttpServletResponse& response)
response.setContentType("text/html"); // 1
std::string output("<html>\n"
"<head><title>Simple Response</title></head>\n"
"<body bgcolor=\"white\">\n"
"<h1>Simple Response</h1>\n"
"A simple HTML response.\n"
"</body></html>\n"); // 2
response.setPayload(output); // 3
//1 Sets the Content-Type of the response to text/html. A client may use the Content-Type to determine the kind of document in the response. Without this line, some clients interpret the response as plain text rather than HTML.
//2 Creates an string containing the response.
//3 Sets the content of the response to output. In this version, the servlet sets the entire response in a single operation.
The related function appendPayload() appends a string to the current contents of the response.