Index: src/edu/ucsb/nceas/metacat/MetaCatServlet.java
===================================================================
--- src/edu/ucsb/nceas/metacat/MetaCatServlet.java	(revision 3210)
+++ src/edu/ucsb/nceas/metacat/MetaCatServlet.java	(revision 3211)
@@ -562,6 +562,7 @@
                 // aware of session expiration on every request
             } else {
                 HttpSession sess = request.getSession(true);
+                logMetacat.info("session.isnew: " + sess.isNew() + "    params.hassessid: " + params.containsKey("sessionid"));
                 if (sess.isNew() && !params.containsKey("sessionid")) {
                     // session expired or has not been stored b/w user requests
                     logMetacat.info(
@@ -568,6 +569,9 @@
                             "The session is new or no sessionid is assigned. The user is public");
                     username = "public";
                     sess.setAttribute("username", username);
+                    sess_id = (String)sess.getId();
+                              sess.setAttribute("sessionid", sess_id);
+                    sessionHash.put(sess_id, sess);
                 } else {
                     logMetacat.info("The session is either old or "
                             + "has sessionid parameter");
@@ -582,17 +586,9 @@
                                 sess = (HttpSession) sessionHash.get(sess_id);
                             }
                         } else {
-                            // we already store the session in login, so we
-                            // don't need here
-                            /*
-                             * logMetacat.info("in no sessionid
-                             * parameter ", 40); sess_id =
-                             * (String)sess.getId();
-                             * logMetacat.info("storing the session id "
-                             * + sess_id + " which has username " +
-                             * sess.getAttribute("username") + " into session
-                             * hash in handleGetOrPost method", 35);
-                             */
+                          //this makes sure there is a session object for public sessions
+                          sess_id = sess.getId();
+                          sessionHash.put(sess_id, sess);
                         }
                     } catch (IllegalStateException ise) {
                         logMetacat.error(
@@ -608,6 +604,8 @@
                     groupnames = (String[]) sess.getAttribute("groupnames");
                     name = (String) sess.getAttribute("name");
                 }
+                
+                logMetacat.info("&*&*&*&*&*&*&*&*&*&*&*&*&*&*&&*&*SESSIONID1: " + sess_id);
 
                 //make user user username should be public
                 if (username == null || (username.trim().equals(""))) {
Index: src/edu/ucsb/nceas/metacat/DBQuery.java
===================================================================
--- src/edu/ucsb/nceas/metacat/DBQuery.java	(revision 3210)
+++ src/edu/ucsb/nceas/metacat/DBQuery.java	(revision 3211)
@@ -52,6 +52,7 @@
 
 import javax.servlet.ServletOutputStream;
 import javax.servlet.http.HttpServletResponse;
+import javax.servlet.http.HttpSession;
 
 import org.apache.log4j.Logger;
 
@@ -257,9 +258,24 @@
                                          String user, String[] groups,
                                          String sessionid, boolean useXMLIndex)
     {
+      int pagesize = 0;
+      int pagestart = 0;
+      
+      if(params.containsKey("pagesize") && params.containsKey("pagestart"))
+      {
+        String pagesizeStr = ((String[])params.get("pagesize"))[0];
+        String pagestartStr = ((String[])params.get("pagestart"))[0];
+        if(pagesizeStr != null && pagestartStr != null)
+        {
+          pagesize = (new Integer(pagesizeStr)).intValue();
+          pagestart = (new Integer(pagestartStr)).intValue();
+        }
+      }
+      
       // get query and qformat
       String xmlquery = ((String[])params.get("query"))[0];
 
+      logMetacat.warn("SESSIONID: " + sessionid);
       logMetacat.warn("xmlquery: " + xmlquery);
       String qformat = ((String[])params.get("qformat"))[0];
       logMetacat.warn("qformat: " + qformat);
@@ -288,7 +304,8 @@
       {
         //xml format
         response.setContentType("text/xml");
-        createResultDocument(xmlquery, qspec, out, user, groups, useXMLIndex);
+        createResultDocument(xmlquery, qspec, out, user, groups, useXMLIndex, 
+          pagesize, pagestart, sessionid);
       }//if
       else
       {
@@ -296,7 +313,8 @@
         response.setContentType("text/html");
         PrintWriter nonout = null;
         StringBuffer xml = createResultDocument(xmlquery, qspec, nonout, user,
-                                                groups, useXMLIndex);
+                                                groups, useXMLIndex, pagesize, 
+                                                pagestart, sessionid);
         
         //transfer the xml to html
         try
@@ -305,7 +323,7 @@
          DBTransform trans = new DBTransform();
          response.setContentType("text/html");
 
-	 // if the user is a moderator, then pass a param to the 
+	       // if the user is a moderator, then pass a param to the 
          // xsl specifying the fact
          if(MetaCatUtil.isModerator(user, groups)){
         	 params.put("isModerator", new String[] {"true"});
@@ -336,7 +354,8 @@
                                             QuerySpecification qspec,
                                             PrintWriter out,
                                             String user, String[] groups,
-                                            boolean useXMLIndex)
+                                            boolean useXMLIndex, int pagesize,
+                                            int pagestart, String sessionid)
   {
     DBConnection dbconn = null;
     int serialNumber = -1;
@@ -361,7 +380,8 @@
         //print out the search result
         // search the doc list
         resultset = findResultDoclist(qspec, resultset, out, user, groups,
-                                      dbconn, useXMLIndex);
+                                      dbconn, useXMLIndex, pagesize, pagestart, 
+                                      sessionid);
 
         
 
@@ -406,10 +426,33 @@
                                       StringBuffer resultsetBuffer,
                                       PrintWriter out,
                                       String user, String[]groups,
-                                      DBConnection dbconn, boolean useXMLIndex )
+                                      DBConnection dbconn, boolean useXMLIndex,
+                                      int pagesize, int pagestart, String sessionid)
                                       throws Exception
     {
+      /*
+      if pagesize != 0 then we need to process the query results in pages
+      1) check to see what the sessionid is: look in MetacatServlet.getSessionHash()
+      2) lookup the sessionid and the query in the paged_results table
+      3) if there is already a page result for the session and query get that
+         result and look at what our pagesize and pagestart is to get the next
+         pagesize results
+      4) if there is not a cached result, do the query, put the result in the 
+         cache under the correct sessionid and return 0..pagesize results
+      5) when the session expires or is logged out, delete the cached queryresults
       
+      
+      paged_results
+      -------------
+      sessionid (PK) (String)
+      query (String)
+      resultset (String)
+      
+      */
+      Hashtable sessionHash = MetaCatServlet.getSessionHash();
+      HttpSession sess = (HttpSession)sessionHash.get(sessionid);
+      //now we have the session object, so we can cache the query there.
+      
       int offset = 1;
       // this is a hack for offset
       if (out == null)
@@ -482,11 +525,13 @@
 
       double startTime = System.currentTimeMillis() / 1000;
       pstmt = dbconn.prepareStatement(query);
-
+      
       // Execute the SQL query using the JDBC connection
       pstmt.execute();
       ResultSet rs = pstmt.getResultSet();
+      
       double queryExecuteTime = System.currentTimeMillis() / 1000;
+      logMetacat.warn("Pagesize: " + pstmt.getFetchSize());
       logMetacat.warn("Time for execute query: "
                     + (queryExecuteTime - startTime));
       boolean tableHasRows = rs.next();
@@ -1516,6 +1561,8 @@
             ignoredParams.add("site");
             ignoredParams.add("operator");
             ignoredParams.add("sessionid");
+            ignoredParams.add("pagesize");
+            ignoredParams.add("pagestart");
 
             // Also ignore parameters listed in the properties file
             // so that they can be passed through to stylesheets
Index: src/edu/ucsb/nceas/metacat/QueryTerm.java
===================================================================
--- src/edu/ucsb/nceas/metacat/QueryTerm.java	(revision 3210)
+++ src/edu/ucsb/nceas/metacat/QueryTerm.java	(revision 3211)
@@ -292,6 +292,7 @@
      */
     public static String useNestedStatements(String pathexpr)
     {
+      System.out.println("pathexpr: " + pathexpr);
         StringBuffer nestedStmts = new StringBuffer();
         Vector nodes = new Vector();
         String path = pathexpr;
