Here's another way to get at a list of parameters within WebFocus. This method allows you to write "generic" focexecs that do not have to know the names of the parameters being passed to the program.
In order to get this to work, you place into Tomcat a "filter", that intercepts each request sent to the WFServlet and copies the names of the parameters in the request into a new multi-valued PARAMS variable which you can then access from within your focexec.
The following useful document describes how tomcat filters work:
http://www-106.ibm.com/developerworks/java/library/j-to...t=grj,p=TomcatTricks You'll want to read the above document to determine how best to configure your tomcat. The following is a sample java filter that creates a "PARAMS" multi-valued parameter. You will probably want to revise this code to work properly at your site. If you find any bugs, please let me know.
// ParamFilter - This class defines a Tomcat Servlet 2.3 Filter
//
// NOTE: This filter will add the PARAMS parameter array to the request.
// ------------------------------------------------------------------------
// 09/29/2004 James Muir Initial coding.
// ------------------------------------------------------------------------
package org.hitchcock.dhdrs.filters;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public final class ParamFilter implements Filter
{
// Data.
private FilterConfig config = null;
// Public Methods.
// init: initialize the filter.
// ------------------------------------------------------------------
public void init (FilterConfig c)
{
this.config = c;
}
// destroy: destroy the filter.
// ------------------------------------------------------------------
public void destroy ()
{
this.config = null;
}
// doFilter: let's do some filtering. We create a subclass of the
// HttpServletRequestWrapper that overrides the getParameter() method
// and it's other friends. Here we create an instance of our custom
// wrapper that wraps the request we originally received. Then we call
// chain.doFilter to pass the wrapper instance instead of the original
// request.
// ------------------------------------------------------------------
public void doFilter (ServletRequest req, ServletResponse res,
FilterChain chain) throws ServletException, IOException
{
ParamRequestWrapper wrapper =
new ParamRequestWrapper((HttpServletRequest)req);
chain.doFilter(wrapper,res);
}
public final class ParamRequestWrapper extends HttpServletRequestWrapper
{
// Data.
private HashMap params = null;
// Constructor.
ParamRequestWrapper (HttpServletRequest req)
{
super(req);
this.params = _setParams((HashMap)req.getParameterMap());
}
// Methods.
// getParameter: returns the value of a request parameter as a String,
// or null if the parameter does not exist. For HTTP servlets,
// parameters are contained in the query string or posted form data.
//
// You should only use this method when you are sure the parameter
// has only one value. If the parameter might have more than one value,
// use getParameterValues(java.lang.String).
//
// If you use this method with a multivalued parameter, the value
// returned is equal to the first value in the array returned by
// getParameterValues.
// ------------------------------------------------------------------
public String getParameter (String name)
{
String[] v = (String[])this.params.get(name);
return (v == null) ? null : v[0];
}
// getParameterNames: returns an Enumeration of String objects
// containing the names of the parameters contained in this request.
// If the request has no parameters, the method returns an empty
// Enumeration.
// ------------------------------------------------------------------
public Enumeration getParameterNames()
{
Vector v = new Vector (this.params.keySet());
return v.elements();
}
// getParameterValues: returns an array of String objects containing
// all of the values the given request parameter has, or null if the
// parameter does not exist. If the parameter has a single value, the
// array has a length of 1.
// ------------------------------------------------------------------
public String[] getParameterValues(String name)
{
return (String[])this.params.get(name);
}
// getParameterMap: returns an immutable java.util.Map containing
// parameter names as keys and parameter values as map values. The
// keys in the parameter map are of type String. The values in the
// parameter map are of type String array.
// ------------------------------------------------------------------
public Map getParameterMap()
{
return (Map)this.params;
}
// _setParams: create a parameters HashMap. The keys in the map are
// of type String. The values in the map are of type String array.
// ------------------------------------------------------------------
private HashMap _setParams(HashMap p)
{
int i = 1;
HashMap map = new HashMap();
map.putAll(p);
Iterator it = p.keySet().iterator();
while(it.hasNext())
{
String key = "PARAMS" + String.valueOf(i++);
_putString( map, key, (String)it.next());
}
_putString( map, "PARAMS0", String.valueOf(i-1));
return map;
}
// _putString: Add an entry to a hash map that has a String as key,
// and a string array with one element as value.
// ------------------------------------------------------------------
private void _putString(HashMap map, String key, String value)
{
String array[] = new String[1];
array[0] = value;
map.put(key, array);
}
}
}
And the following is a sample focexec that displays the parameters sent it using the &PARAMS variable:
-* Echo parameters in form.
-*
-SET &LIMIT = &PARAMS0;
-REPEAT LOOP FOR &COUNTER FROM 1 TO &LIMIT STEP 1
-SET &MYPARM1 = 'PARAMS' | &COUNTER;
-SET &MYPARM2 = '&' || &MYPARM1;
-SET &MYPARM3 = '&' || &MYPARM2.EVAL;
-IF &MYPARM3.EVAL.EXISTS EQ 0 THEN GOTO NULLVALUE;
-TYPE &MYPARM1.EVAL: NAME=&MYPARM2.EVAL VALUE=&MYPARM3.EVAL
-GOTO NEXT
-NULLVALUE
-TYPE &MYPARM1.EVAL: NAME=&MYPARM2.EVAL VALUE=NULL
-GOTO NEXT
-NEXT
-LOOP
-EXIT