Java Reference
In-Depth Information
+ foodName + "">");
html.append("<input type=submit value=Buy!>");
html.append("</form>");
html.append("</body>");
html.append("</html>");
}
The POST method takes the parameters passed across from the GET method, looks up a
Shop service, and puts the purchase through.
Listing 3.10
The doPost() method of the BuyFood servlet
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
PrintWriter html = response.getWriter();
html.append("<html>");
String customerName = request.getParameter("customerName");
String foodName = request.getParameter("foodName");
String quantity = request.getParameter("quantity");
try {
InitialContext ctx = new InitialContext();
Shop shop = (Shop) ctx.lookup("osgi:service/"
+ Shop.class.getName());
Inventory inventory = (Inventory) ctx.lookup(
"osgi:service/" + Inventory.class.getName());
Accounting accounting = (Accounting) ctx.lookup(
"osgi:service/" + Accounting.class.getName());
Customer customer = accounting.getCustomer(customerName);
if (customer != null) {
html.append(customer + "<br/>");
} else {
html.append(customerName + " is a new customer.<br/>");
}
Food food = inventory.getFood(foodName);
html.append(food + "<br/>");
html.append(customerName + " tried to buy " + quantity
+ " packs of " + foodName + "<br/>");
try {
shop.purchase(foodName, customerName,
Integer.valueOf(quantity));
Customer refreshedCustomer = accounting
.getCustomer(customerName);
html.append("Afterwards, " + refreshedCustomer +
"<br/>");
} catch (Exception e) {
html.append("A problem happened: " + e.getMessage() +
"<br/>");
}
 
Search WWH ::




Custom Search