Index: src/edu/ucsb/nceas/metacat/MetaCatServlet.java
===================================================================
--- src/edu/ucsb/nceas/metacat/MetaCatServlet.java	(revision 369)
+++ src/edu/ucsb/nceas/metacat/MetaCatServlet.java	(revision 370)
@@ -79,18 +79,18 @@
  */
 public class MetaCatServlet extends HttpServlet {
 
-  private ServletConfig		config = null;
-  private ServletContext	context = null;
-  private Hashtable 		connectionPool = new Hashtable();
-  private String 		resultStyleURL = null;
-  private String 		xmlcatalogfile = null;
-  private String 		saxparser = null;
-  private String        defaultdatapath = null; 
-  private String        servletpath = null; 
-  private String        htmlpath = null; 
+  private ServletConfig config = null;
+  private ServletContext context = null;
+  private Hashtable connectionPool = new Hashtable();
+  private String resultStyleURL = null;
+  private String xmlcatalogfile = null;
+  private String saxparser = null;
+  private String defaultdatapath = null; 
+  private String servletpath = null; 
+  private String htmlpath = null; 
 					// path to directory where data files 
 					// that can be downloaded will be stored
-  private String        executescript  = null;  
+  private String executescript  = null;  
 					// script to get data file and put it 
                     // in defaultdocpath dir
   private PropertyResourceBundle options = null;
@@ -244,20 +244,6 @@
     }    
 // End of Jivka added
 
-    try
-    {
-      Class[] x = {Class.forName("PrintWriter"), 
-                   Class.forName("Hashtable"), 
-                   Class.forName("HttpServletResponse")};
-      Class c = Class.forName("edu.ucsb.nceas.metacat.MetaCatServlet");
-      Method m = c.getDeclaredMethod("handleQueryAction", x);
- 
-    }
-    catch(Exception e)
-    {
-       
-    }
-
     if(action.equals("query"))
     {
       handleQuery(out, params, response); 
@@ -410,20 +396,24 @@
             out.println("<error>");
             out.println("SRB Connection failed. " + re.getMessage());
             out.println("</error>"); 
-    }        
-  }
-
-  /** 
-    *Create the squery xml and return it as a String.
+    }             
+  }    
+  
+  /**  
+    *Create the squery xml, execute it and display it
+    * @param out is the output stream to the client
     * @param params is the Hashtable of parameters that should be included
     * in the squery.
+    * @param response is the response object linked to the client
+    * @param conn the database connection 
     */
-  private void handleSQuery(PrintWriter out, Hashtable params, 
-                            HttpServletResponse response) 
-  {
+  private void handleSQuery(PrintWriter out, Hashtable params,  
+                            HttpServletResponse response)    
+  {  
     //create the squery and return it to be processed
     String doctype = null;
     String[] doctypeArr = null;
+    Connection conn = null;
     doctypeArr = (String[])params.get("doctype");
     doctype = null;
     if (doctypeArr != null) 
@@ -436,16 +426,32 @@
     }
     String xmlquery = DBQuery .createSQuery(params, doctype);
     Hashtable doclist = runQuery(xmlquery);
-    String qformat = ((String[])params.get("qformat"))[0]; 
-    transformDocument(doclist, qformat, xmlquery, out, response);
+    String qformat = ((String[])params.get("qformat"))[0];
+    String resultdoc = createResultDocument(doclist, qformat, xmlquery, 
+                                            out, response);
+    if(qformat.equals("html"))
+    {
+      transformResultset(resultdoc, response, out);
+    }
+    else if(qformat.equals("xml"))
+    {
+      response.setContentType("text/xml");
+      out.println(resultdoc);
+    }
+    else
+    {
+      out.println("invalid qformat: " + qformat); 
+    }
+    //conn.close();
   }
   
    /**
-    *Create the query xml and return it as a String.
-    * @param query is the free text query parameter returned through the CGI.
-    * @param doctype is the doctype parameter returned through the CGI. 
-    * If no doctype filter is required, set it to null or "".
-    */
+    *Create the xml query, execute it and display the results.
+    * @param out is the output stream to the client
+    * @param params is the Hashtable of parameters that should be included
+    * in the squery.
+    * @param response is the response object linked to the client
+    */ 
   private void handleQuery(PrintWriter out, Hashtable params, 
                            HttpServletResponse response)
   {
@@ -452,8 +458,10 @@
     String doctype=null; 
     String[] doctypeArr=null; 
     String query=null;
+    Connection conn = null;
+    
     if(params.containsKey("query"))
-    {
+    { //get the query parameter
       query = ((String[])params.get("query"))[0];
     }
     else
@@ -462,7 +470,7 @@
     }
     
     if(params.containsKey("doctype"))
-    {
+    { //check for the doctype
       doctypeArr = (String[])params.get("doctype");
       doctype = null;
     }
@@ -479,14 +487,33 @@
     {
       doctype="ANY"; 
     }
+    
+    //create the query and run it
     String xmlquery = DBQuery.createQuery(query, doctype);
     Hashtable doclist = runQuery(xmlquery);
     String qformat = ((String[])params.get("qformat"))[0]; 
-    transformDocument(doclist, qformat, xmlquery, out, response);
+    String resultdoc = createResultDocument(doclist, qformat, xmlquery, 
+                                            out, response);
+    //format and transform the results                                        
+    if(qformat.equals("html"))
+    {
+      transformResultset(resultdoc, response, out);
+    }
+    else if(qformat.equals("xml"))
+    {
+      response.setContentType("text/xml");
+      out.println(resultdoc);
+    }
+    else
+    {
+      out.println("invalid qformat: " + qformat); 
+    }
+    //conn.close();
   }
   
   /**
     * Run the query and return a hashtable of results.
+    * @param xmlquery the query to run
     */
   private Hashtable runQuery(String xmlquery)
   {
@@ -512,6 +539,36 @@
     }    
   }
   
+   /**
+   * Transorms an xml resultset document to html and sends it to the browser
+   * @param resultdoc the string representation of the document that needs
+   * to be transformed.
+   * @param response the HttpServletResponse object bound to the client.
+   * @param out the output stream to the client
+   */
+  private void transformResultset(String resultdoc, 
+                                  HttpServletResponse response,
+                                  PrintWriter out)
+  {
+    Connection conn = null;
+    try
+    {
+      conn = util.getConnection();
+      DBTransform trans = new DBTransform(conn);
+      response.setContentType("text/html");
+      trans.transformXMLDocument(resultdoc, "-//NCEAS//resultset//EN", 
+                                 "-//W3C//HTML//EN", out);
+    }
+    catch(Exception e)
+    {
+      if (conn != null) 
+      {
+        util.returnConnection(conn); 
+      }
+    } 
+    //conn.close();
+  }
+  
   /**
    * Transforms a hashtable of documents to an xml or html result.
    * @param doclist- the hashtable to transform
@@ -520,7 +577,7 @@
    * @param out- the printwriter object used to write to the client
    * @param response- the response stream to write back to the client.
    */
-  private void transformDocument(Hashtable doclist, String qformat, 
+  private String createResultDocument(Hashtable doclist, String qformat, 
                                  String xmlquery, PrintWriter out,
                                  HttpServletResponse response)
   {
@@ -541,7 +598,8 @@
       resultset.append("  <document>" + document + "</document>");
     }
     resultset.append("</resultset>");
-
+    return resultset.toString();
+    /*
     if(qformat.equals("xml")) 
     {
       // set content type and other response header fields first
@@ -566,6 +624,7 @@
         out.println("Error transforming document:\n" + e.getMessage());
       }
     }
+    */
   }
 
   /** 
@@ -933,6 +992,9 @@
 
 /**
  * '$Log$
+ * 'Revision 1.71  2000/08/15 20:48:05  berkley
+ * 'remove handleQueryAction() in favor of directly calling handleQuery() and handleSQuery() from doGetOrPost()
+ * '
  * 'Revision 1.70  2000/08/15 20:02:15  bojilova
  * 'Cleared hardcoded paths for the location of .html files and use
  * 'the new "htmlpath" property from metacat.properties file
