|
Programmers Guide > An Example
A JOT Servlet Example
This JOT Servlet example provides dynamic content for the
JOT Template example.
The
JotResponseBean method getCustomerName() is used by the
JOT.CustomerName token. The JOT Iterator Items inner class provides
dynamic content for the list of items. A property named "Items" is created.
package com.jotobjects.demo.servlet;
import java.util.*;
import java.text.DecimalFormat;
import com.jotobjects.JotException;
import com.jotobjects.servlet.*;
import com.jotobjects.content.JotCollectionIteratorBean;
/** ViewShoppingCart is a JOT Servlet with an inner JotResponseBean
* and an inner JotTemplateIterator. The Items list is a java.util.List
* stored in the HttpSession.
*/
public class ViewShoppingCart
extends JotHttpServlet
implements JotQueryServlet
{
private static DecimalFormat fmt = new DecimalFormat("#,##0.0#");
static public class Bean
extends JotServletResponseBean
{
private Customer customer;
public void prepareJotBean()
throws JotException
{
super.prepareJotBean();
String cid = (String)getJotBeanProperty("customerID"); // Form field
customer = new Customer(cid);
addJotBeanProperty("Items", new Items()); // property for JOT.Items token
}
// CustomerName JotBean property used by JOT.CustomerName token
public String getCustomerName() { return customer.getName(); }
// JOT Iterator provides list of purchased items.
public class Items
extends JotCollectionIteratorBean
{
double totalPurchase = 0.0;
Items()
throws JotException
{
List itemsList = hasJotBeanProperty("purchases") // Session attribute
? (List)getJotBeanProperty("purchases")
: Collections.EMPTY_LIST;
setCollection(itemsList);
}
public String getTotalPurchase()
{
return fmt.format(totalPurchase);
}
public String getProductName()
{
return ((Product)getElement()).name;
}
public String getProductPrice()
{
Product product = (Product)getElement();
totalPurchase += product.price;
return fmt.format(product.price);
}
}
}
}
|