Java Reference
In-Depth Information
Map<String, String> form = formToMap(formData);
// create a new product based on form data
Product product = new Product();
product.setId(form.get("id"));
product.setName(form.get("name"));
product.setQty(Integer.parseInt(form.get("qty")));
cartProducts.add(product);
System.out.println("Returning all products.");
return cartProducts;
}
/*
* Now that we have form data in a single string, parse it
* to find the key/value pairs, bust them up, and put them
* in a map.
*/
private Map<String, String> formToMap(String formData)
throws IOException {
System.out.println("Breaking string of form into map.");
Map<String, String> form = new HashMap<String, String>();
String[] keyValuePairs = formData.split("&");
for (int i = 0; i < keyValuePairs.length; i++) {
String[] s = keyValuePairs[i].split("=");
form.put(URLDecoder.decode(s[0], ENC),
URLDecoder.decode(s[1], ENC));
}
return form;
}
/*
* Helper converts the form data input stream to a string
* so we can read its values.
*/
private String formToString(InputStream is) throws IOException {
System.out.println("getting request input as string.");
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line;
Search WWH ::




Custom Search