Java Reference
In-Depth Information
"<option>1</option><option>2</option>" +
"<option>3</option><option>4</option></select>" +
"</td></tr>" +
"<tr><td></td>" +
"<td><input type='submit' value='Add to Order' name='submit'
/>" +
"</td></tr></table>";
private static String ORDER_EMPTY_HEADER =
"<p>There are no items in your order.</p>";
private static String ITEMS_HEADER =
"<p>There are %d items in your order:</p>" +
"<table>" +
"<tr><th>ID</th><th>Name</th><th>Quantity</th></tr>";
private static String END_HTML = "</body></html>";
}
Once the runtime determines that the
ProductHtmlFormWriter
class is the appropriate hand-
ler for the incoming request (by invoking the
isWriteable
method), the
writeTo
method
is invoked. The
@Produces
annotation indicates that the class creates HTTP responses. It is
responsible for creating the HTTP response body. There are a few static strings that contain
boilerplate HTML that make up the form, which you use to keep the main body of the method
clear. All the
writeTo
method does is loop over the list of accumulated products and write
them out as a table, and then present the HTML form.
After the user fills out the form and clicks Submit, the reader eventually gets invoked to
accept the submitted form data and do something with it. The
ProductHtmlFormReader
class
is shown in
Example 8-36
.
Example8-36.TheProductHtmlFormReaderclassacceptsincomingHTMLformdataand
parses it
package com.soacoobook.rest.order;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;