You are viewing a plain text version of this content. The canonical link for it is here.
Posted to ojb-dev@db.apache.org by to...@apache.org on 2004/06/24 21:50:37 UTC

cvs commit: db-ojb/src/samples/org/apache/ojb/servlet TestServlet.java

tomdz       2004/06/24 12:50:37

  Modified:    src/samples/org/apache/ojb/servlet TestServlet.java
  Log:
  Fixed sample servlet
  
  Revision  Changes    Path
  1.3       +29 -27    db-ojb/src/samples/org/apache/ojb/servlet/TestServlet.java
  
  Index: TestServlet.java
  ===================================================================
  RCS file: /home/cvs/db-ojb/src/samples/org/apache/ojb/servlet/TestServlet.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestServlet.java	22 Jun 2004 22:34:01 -0000	1.2
  +++ TestServlet.java	24 Jun 2004 19:50:37 -0000	1.3
  @@ -30,6 +30,7 @@
   
   import java.io.IOException;
   import java.io.PrintWriter;
  +import java.util.Collection;
   import java.util.Date;
   import java.util.Iterator;
   
  @@ -53,11 +54,26 @@
       protected void doGet(HttpServletRequest req, HttpServletResponse response)
           throws ServletException, IOException
       {
  -       	PrintWriter writer = response.getWriter();
  +        PersistenceBroker broker = null;
  +
  +        try
  +        {
  +            broker = getBroker();
  +        }
  +        catch (Exception ex)
  +        {
  +            throw new IOException(ex.getMessage()); 
  +        }
  +
  +        response.setContentType("text/html");
  +        
  +        PrintWriter writer = response.getWriter();
   
           writeHeader(writer);
  -      	writeTable(getTableIterator(), writer);
  +      	writeTable(getProducts(broker), writer);
          	writeFooter(writer);
  +
  +        broker.close();
       }
   
       /**
  @@ -87,18 +103,18 @@
       /**
        * Writes a table containg the given products to the writer.
        * 
  -     * @param iter   An iterator of the products to write
  -     * @param writer The writer
  +     * @param products The products to write
  +     * @param writer   The writer
        */
  -    protected void writeTable(Iterator iter, PrintWriter writer)
  +    protected void writeTable(Collection products, PrintWriter writer)
       {
           writer.println("<table border=\"1\">");
           writer.println("<tr><td><b>ID</b></td> <td><b>NAME</b></td> <td><b>PRICE</b></td> <td><b>STOCK</b></td></tr>");
  -        if (iter != null)
  +        if (products != null)
           {
  -            while (iter.hasNext())
  +            for (Iterator it = products.iterator(); it.hasNext();)
               {
  -             	Product a = (Product)iter.next();
  +             	Product a = (Product)it.next();
       
                   writer.println("<tr><td>" + a.getId() + "</td> <td>" + a.getName() + "</td> <td>" + a.getPrice()+ "</td> <td>" + a.getStock() + "</td></tr>");   
               }
  @@ -110,28 +126,14 @@
       /**
        * Retrieves the products to write.
        * 
  -     * @return An iterator of the products
  +     * @return The products
        */
  -    protected Iterator getTableIterator() throws IOException
  +    protected Collection getProducts(PersistenceBroker broker) throws IOException
       {
  -        Iterator          result = null;
  -        PersistenceBroker broker = null;
  -
  -        try
  -        {
  -        	broker = getBroker();
  -        }
  -        catch (Exception ex)
  -        {
  -        	throw new IOException(ex.getMessage());	
  -        }
  -
  -        Criteria selectAll = null;
  -        Query    query     = QueryFactory.newQuery(Product.class, selectAll);
  +        Criteria   selectAll = null;
  +        Query      query     = QueryFactory.newQuery(Product.class, selectAll);
   
  -        result = broker.getIteratorByQuery(query);
  - 		broker.close();
  -     	return result;   
  +        return broker.getCollectionByQuery(query);
       }
   
       /**
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: ojb-dev-unsubscribe@db.apache.org
For additional commands, e-mail: ojb-dev-help@db.apache.org