Java Reference
In-Depth Information
StringBuilder body = new StringBuilder();
body.append("<html><head><title>");
body.append(code + ":" + message);
body.append("</title></head><body><p>An error occured.</p><h1>");
body.append(code);
body.append("</h1><p>");
body.append(message);
body.append("</p></body></html>");
This HTML page is then converted into an array of bytes. Next, this array of bytes,
along with the code and message , is passed to the transmit method. Finally, the
transmit method will send this data to the web browser.
transmit(out, code, message, body.toString().getBytes(),
"text/html");
The error method is handy because it can be called from several different locations
when an error occurs.
The Transmit Method
Both the error and sendFile methods use the transmit method to actually
send the page to the web browser. This is very convenient because the transmit method
properly handles all of the HTTP headers, and thus saves both the error and sendFile
methods from both having to implement this functionality.
First, the HTTP headers are constructed. The HTTP headers are constructed into a
StringBuilder , as seen here:
StringBuilder headers = new StringBuilder();
headers.append("HTTP/1.1 ");
headers.append(code);
headers.append(' ');
headers.append(message);
headers.append("\n");
headers.append("Content-Length: " + body.length + "\n");
headers.append("Server: Heaton Research Example Server\n");
headers.append("Connection: close\n");
headers.append("Content-Type: " + content + "\n");
headers.append("\n");
Once the headers have been constructed, both the header and body can be transmit-
ted. This is done using the following two commands. These commands make use of the
OutputStream and write the necessary data.
out.write(headers.toString().getBytes());
out.write(body);
Search WWH ::




Custom Search