Index: /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/LibraryServlet.java
===================================================================
--- /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/LibraryServlet.java	(revision 33611)
+++ /main/trunk/greenstone3/src/java/org/greenstone/gsdl3/LibraryServlet.java	(revision 33612)
@@ -1,2 +1,21 @@
+/*
+ *    LibraryServlet.java
+ *    Copyright (C) 2008 New Zealand Digital Library, http://www.nzdl.org
+ *
+ *    This program is free software; you can redistribute it and/or modify
+ *    it under the terms of the GNU General Public License as published by
+ *    the Free Software Foundation; either version 2 of the License, or
+ *    (at your option) any later version.
+ *
+ *    This program is distributed in the hope that it will be useful,
+ *    but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *    GNU General Public License for more details.
+ *
+ *    You should have received a copy of the GNU General Public License
+ *    along with this program; if not, write to the Free Software
+ *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
+ */
+
 package org.greenstone.gsdl3;
 
@@ -7,4 +26,5 @@
 import java.lang.reflect.Type;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.Enumeration;
 import java.util.HashMap;
@@ -52,5 +72,6 @@
 /**
  * a servlet to serve the greenstone library - we are using servlets instead of
- * cgi the init method is called only once - the first time the servlet classes
+ * cgi 
+ * the init method is called only once - the first time the servlet classes
  * are loaded. Each time a request comes in to the servlet, the session() method
  * is called in a new thread (calls doGet/doPut etc) takes the a=p&p=home type
@@ -69,4 +90,9 @@
 	protected Receptionist recept = null;
 
+  /** We record the library name for later */
+  protected String library_name = null;
+	/** Whether or not client-side XSLT support should be exposed */
+	protected boolean supports_client_xslt = false;
+
 	/**
 	 * the default language - is specified by setting a servlet param, otherwise
@@ -74,10 +100,4 @@
 	 */
 	protected String default_lang = null;
-
-  /** We record the library name for later */
-  protected String library_name = null;
-	/** Whether or not client-side XSLT support should be exposed */
-	protected boolean supports_client_xslt = false;
-
 	/**
 	 * The default default - used if a default lang is not specified in the
@@ -304,8 +324,7 @@
 		//Allow the message router and the document to be accessed from anywhere in this servlet context
 		this.getServletContext().setAttribute(library_name+"Router", this.recept.getMessageRouter());
-		//this.getServletContext().setAttribute("LibraryName", library_name);
 	}
 
-	private void logUsageInfo(HttpServletRequest request)
+  private void logUsageInfo(HttpServletRequest request, Map<String, String[]> queryMap)
 	{
 		String usageInfo = "";
@@ -320,11 +339,21 @@
 		}
 
+		String queryParamStr = "";
+		if (queryMap != null)
+		{
+			Iterator<String> queryIter = queryMap.keySet().iterator();
+			while (queryIter.hasNext()) {
+			  String q = queryIter.next();
+			  String[] vals = queryMap.get(q);
+			  queryParamStr += q +"="+String.join(",",vals)+" ";
+			}
+		}
 		//logged info = general-info + session-info
 		usageInfo = request.getServletPath() + " " + //serlvet
-		"[" + request.getQueryString() + "]" + " " + //the query string
-		"[" + usageInfo.trim() + "]" + " " + // params stored in a session
-		request.getRemoteAddr() + " " + //remote address
-		request.getRequestedSessionId() + " " + //session id
-		request.getHeader("user-agent") + " "; //the remote brower info
+		  "[" + queryParamStr.trim()+"]"+" " + // query map params 
+		  "session:[" + usageInfo.trim() + "]" + " " + // params stored in a session
+		  request.getRemoteAddr() + " " + //remote address
+		  request.getRequestedSessionId() + " " + //session id
+		  request.getHeader("user-agent") + " "; //the remote brower info
 
 		logger.info(usageInfo);
@@ -367,4 +396,5 @@
 		}
 
+	  // if session cache id removed from session, this triggers this valueUnbound method on the value=this object
 		public void valueUnbound(HttpSessionBindingEvent event)
 		{
@@ -388,5 +418,5 @@
 	public void doGetOrPost(HttpServletRequest request, HttpServletResponse response, Map<String, String[]> queryMap) throws ServletException, IOException
 	{
-		logUsageInfo(request);
+	  logUsageInfo(request, queryMap);
 
 		if (queryMap != null)
@@ -541,5 +571,5 @@
 
 		// We clean up the cache session_ids_table if system
-		// commands are issued (and also don't need to do caching for this request)
+		// commands are issued, and also don't need to do caching for these request requests
 		boolean should_cache = true;
 		if (action != null && action.equals(GSParams.SYSTEM_ACTION) 
@@ -578,5 +608,5 @@
 			    // TODO
 				session_ids_table = new Hashtable<String, UserSessionCache>();
-				session.removeAttribute(GSXML.USER_SESSION_CACHE_ATT); // triggers valueUnbound(), which removes the session id from the session_ids_table
+				session.removeAttribute(GSParams.USER_SESSION_CACHE); // triggers valueUnbound(), which removes the session id from the session_ids_table
 			}
 			else
@@ -604,14 +634,14 @@
 		}
 		
-		//clear the collection-specific cache in the session, since we have no way to know whether this session is 
-		//about the same collection as the last session or not.
+		//clear the collection-specific cache in the session, since we have no way to know whether this page is 
+		//about the same collection as the last page or not.
 		Enumeration attributeNames = session.getAttributeNames();
 		while (attributeNames.hasMoreElements())
 		{
 			String name = (String) attributeNames.nextElement();
-			if (!name.equals(GSXML.USER_SESSION_CACHE_ATT) && !name.equals(GSParams.LANGUAGE) && !name.equals(GSXML.USER_ID_ATT))
+			if (!name.equals(GSParams.USER_SESSION_CACHE) && !name.equals(GSParams.LANGUAGE) && !name.equals(GSXML.USER_ID_ATT) && !this.gs_params.isGlobal(name))
 			{
 				session.removeAttribute(name);
-			}
+			} 
 		}
 
@@ -621,7 +651,11 @@
 		boolean new_table = false;
 		String sid = session.getId();
+		boolean new_session = false;
+		if (!session_ids_table.containsKey(sid)) {
+		  new_session = true;
+		}
 		if (should_cache == true && cache_key != null && !cache_key.equals(""))
 		{
-			if (session_ids_table.containsKey(sid))
+			if (!new_session)
 			{
 				session_cache = session_ids_table.get(sid);
@@ -640,4 +674,5 @@
 			else
 			{
+			  
 				param_table = new Hashtable<String, Hashtable<String, String>>();
 				table = new Hashtable<String, String>();
@@ -645,26 +680,37 @@
 				session_cache = new UserSessionCache(sid, param_table);
 				session_ids_table.put(sid, session_cache);
-				session.setAttribute(GSXML.USER_SESSION_CACHE_ATT, session_cache);
+				session.setAttribute(GSParams.USER_SESSION_CACHE, session_cache);
 				new_table = true;
 			}
 
-			// here we add in default params if we have a new table
-			ArrayList<String> defp = this.gs_params.getParamsWithDefaults();
-			Iterator i = this.gs_params.getParamsWithDefaults().iterator();
-			while (i.hasNext()) {
-			  String p = (String)i.next();
-			  String v = this.gs_params.getParamDefault(p);
-			  if (!p.equals(GSParams.ACTION) && !p.equals(GSParams.SUBACTION) && !p.equals(GSParams.LANGUAGE) && !p.equals(GSParams.OUTPUT)) {
-			    table.put(p,v);
-			  }
-			}
-			
+		}
 		
-		}
+		// here we add in default params values if need be - if we have a new session, or if we have a new table
+		// in an existing session
+		// don't override any existing values
+		if (new_session || new_table) {
+
+		  Iterator i = this.gs_params.getParamsWithDefaults().iterator();
+		  while (i.hasNext()) {
+		    String p = (String)i.next();
+		    String v = this.gs_params.getParamDefault(p);
+		      if (this.gs_params.isGlobal(p)) {
+			//need to add in to session unless its already there
+			if (session.getAttribute(p) == null) {
+			  session.setAttribute(p, v);
+			}
+		      } else {
+			// add to table unless its already there
+			if (new_table && !table.containsKey(p)) {
+			  table.put(p,v);
+			}
+		      }
+		    
+		  }
+	        }
 
 		if (action == null || action.equals(""))
 		{
-			// should we do all the following stuff if using default page?
-			// display the home page  - the default page
+		        // display the home page  - the default page
 			xml_request.setAttribute(GSXML.ACTION_ATT, "p");
 			xml_request.setAttribute(GSXML.SUBACTION_ATT, PageAction.HOME_PAGE);
@@ -677,5 +723,6 @@
 				xml_request.setAttribute(GSXML.SUBACTION_ATT, subaction);
 			}
-
+		}
+		
 			//  create the param list for the greenstone request - includes
 			// the params from the current request and any others from the saved session
@@ -702,8 +749,12 @@
 						}
 					}
-					// either add it to the param list straight away, or save it to the session and add it later
-					if (this.gs_params.shouldSave(name) && table != null)
-					{
-						table.put(name, value);
+					// if we need to save the value, then add it to the session/cache table.
+					// otherwise add directly to the paramList
+					if (this.gs_params.shouldSave(name)) {
+					  if (this.gs_params.isGlobal(name) || table == null) {
+					    session.setAttribute(name, value);
+					  } else {
+					    table.put(name, value);
+					  } 
 					}
 					else
@@ -721,5 +772,4 @@
 			}
 			//put everything in the table into the session
-			// do we need to do this? why not just put from table into param list
 			if (table != null)
 			{
@@ -737,5 +787,5 @@
 			{
 				String name = (String) params.nextElement();
-				if (!name.equals(GSXML.USER_SESSION_CACHE_ATT) && !name.equals(GSParams.LANGUAGE) && !name.equals(GSXML.USER_ID_ATT))
+				if (!name.equals(GSParams.USER_SESSION_CACHE) && !name.equals(GSParams.LANGUAGE) && !name.equals(GSXML.USER_ID_ATT))
 				{
 
@@ -751,5 +801,5 @@
 				}
 			}
-		}
+	
 
 		if (output.equals("json"))
