| |
"Design Patterns for Optimizing the Performance of J2EE Applications"
Vol. 6, Issue 9, p. 48
Listing 1: Servlet that uses a CatalogEJB to read a list of objects
// All required imports
public class DisplayServices extends HttpServlet {
// variable declarations
public void init() {
// all inits required
}
public void doPost(HttpServletRequest request, HttpServletResponse
response)
throws IOException, ServletException {
// other processing, if any
InitialContext ctxt = new InitialContext();
Object obj = ctxt.lookup("java:comp/env/ejb/Catalog/Services");
CatalogHome catHome = (CatalogHome)
PortableRemoteObject.narrow(obj, CatalogHome.class);
Catalog catList = catHome.create();
Collection list = catList.getServiceNames();
while(int i=0; i < list.size(); i++) {
// get the service name
// display the service name received
}
// do other processing required
}
// other methods definitions
}
// The following is the sample code of the CatalogEJB
// For simplicity sake, the definitions of the home and remote
// interfaces are not shown
// All required imports
public class CatalogEJB implements EntityBean {
// Methods like ejbCreate etc go here
// Business methods start here
public Collection getServiceNames() {
// Access the database and get the list of service names
// Form a collection of the service names
// Return this collection
}
public int updateServices(arg1, arg2) {
// this method will allow the CatalogEJB to update the list of
Services;
// for example, this method might build the SQL query for updating
the
// list services in the catalog and use JDBC to execute the query
}
public int addServices(arg1, arg2) {
// this method will allow the CatalogEJB to add to the list of
Services
}
// Other business methods
}
Listing 2: Data Access Object that encapsulates all types of access to the Catalog
// all required imports
public class CatalogDataAccessObject {
private Connection dbConnection;
private DataSource datasource;
public CatalogDataAccessObject() {
// appropriate exceptions to be caught
InitialContext ic = new InitialContext();
datasource = (DataSource)
ic.lookup("java:comp/env/jdbc/CatalogDataSource");
dbConnection = datasource.getConnection();
}
public int updateServices(arg1, arg2) {
// this method will allow the CatalogEJB to update the list of
Services;
// for example, this method might build the SQL query for updating
the
// list services in the catalog and use JDBC to execute the query
}
public int addServices(arg1, arg2) {
// this method will allow the CatalogEJB to add to the list of
Services
}
public Collection getServiceNames() {
// this method will allow the catalogEJB or any client
using Fast Lane
// Reader pattern to get the list of services
}
// other method definitions as required
}
Listing 3: Servlet that uses Fast Lane Reader pattern
// All required imports
public class DisplayServices extends HttpServlet {
private CatalogDataAccessObject dao;
// other declarations
public void init() {
CatalogDataAccessObject dao = new
CatalogDataAccessObject();
// all other inits required
}
public void doPost(HttpServletRequest reqest,
HttpServletResponse response)
throws IOException, ServletException {
// other processing, if any
Collection list = dao.getServiceNames();
for(int i=0; i<list.size(); list++) {
// get the service name received
// display the list name
}
// do other processing as required
}
// other method definitions, if any
}
Listing 4: Page-by-Page Iterator pattern returned to the client
// all required imports
public class PageByPageIteratorImpl {
/*
"start" represents the starting index of the first element of this
sublist.
*/
private int start;
/*
"size" represents the number of elements that are in this sublist.
*/
private int size;
/*
"totalCount" represents the total number of objects that would
have been
returned in the absence of the Page-By-Page Iterator; very useful
information
for the clients in deciding to iterate more or not
*/
private int totalCount;
/*
"objs" has the actual collection of objects
*/
private Collection objs;
public PageByPageIteratorImpl(int start, int size, int total,
Collection
coll) {
this.start = start;
this.size = size;
this.totalCount = total;
this.objs = coll;
}
public int getStartIndex() {
return start;
}
// Other similar accessor methods for size, totalCount, objs
}
Listing 5: Data Access Object
public PageByPageIteratorImpl getServiceNames(int startIndex,
int size) {
// get the total count of the number of service into
variable "total";
// access the database and get the collection of service
names in
// "retObjs";
// the collection obtained will be of requested size only
starting from
// the index specified;
// return the collection of service names as part of the
Page-by-Page Iterator
return(new PageByPageInteratorImpl(startIndex, size,
total, retObjs));
}
// other method definitions as required
}
Listing 6: Servlet using Fast Lane Reader and Page-by-Page Iterator
// All required imports
public class DisplayServices extends HttpServlet {
private CatalogDataAccessObject dao;
// other declarations
public void init() {
CatalogDataAccessObject dao = new
CatalogDataAccessObject();
// all other inits required
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// other processing, if any
Integer startIndex = new
Integer(request.getParameter("StartIndex"));
Integer listSize = new
Integer(request.getParameter("ListSize"));
PageByPageIteratorImpl list =
dao.getServiceNames(startIndex.intValue(),
listSize.
intValue());
Collection objs = list.getCollection();
// Now the list received is of requested size only
// iterate thro the list and display the service names
received
// do other processing as required
}
// other method definitions
}
Listing 7: Value Object class for a service
// all imports required
public class ServiceInformation implements java.io.Serializable {
private String serviceName;
private double serviceCost;
private String[] serviceTerms;
// other attributes of a service
public ServiceInformation(String name, double cost, String[]
terms) {
serviceName = name;
serviceCost = cost;
serviceTerms = terms;
}
public String getName() {
return(serviceName);
}
public double getCost() {
return(serviceCost);
}
public String[] getTerms() {
return(serviceTerms);
}
// similar accessor methods for other attributes
}
Listing 8: Using all these patterns discussed
// All required imports
public class DisplayServices extends HttpServlet {
private CatalogDataAccessObject dao;
// other declarations
public void init() {
CatalogDataAccessObject dao = new
CatalogDataAccessObject();
// all other inits required
}
public void doPost(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
// other processing, if any
Integer startIndex = new
Integer(request.getParameter("StartIndex"));
Integer listSize = new
Integer(request.getParameter("ListSize"));
PageByPageIteratorImpl list =
dao.getServiceNames(startIndex.intValue(),
listSize.intValue());
Collection objs = list.getCollection();
// The collection is a collection of the value objects
// Now the list received is of requested size only
// extract the service names and the attributes from the
value objects
// display everything and do other processing as required
}
// other method definitions
}
|
|