Search



Query String Parser

Skip to article content
Article information
  • Edit
  •  LotusScript  ,  Programming  ,  Web applications 
    Lotus Script Query_String
    Ernest Leyva
    11/06/2008
    Ernest Leyva
    11/06/2008
     


    Many times when we do lotus domino development, we need to send parameters to the server. How do we do this? well using a url like this:

    http://www.something.com/resource?command&param1=val1&param2=val2

    If this is pointing to a form, then we all need to do is call:

    @URLQueryString

    But sometimes, we want to call an agent and do some processing there, in this case we can use the function below to parse the query_string field:

    Plesea note, the way we get the query string in an agent would it be: session.documentcontext.query_string(0)


    Function parseQS( queryString As String, params List As String ) As Integer
    Dim buffer As Variant, pairs As Variant, ceiling As Integer, i As Integer
    On Error Resume Next
    Erase params
    buffer = Split( queryString , "&" )
    ceiling = Ubound( buffer )
    For i=0 To ceiling
    pairs= Split( buffer(i), "=" )
    params( pairs(0) ) = pairs(1)
    Next
    parseQS = ceiling
    End Function


    The first parameter is the queryString field returned by Domino. The second is an output parameter, a list of strings, that will contain each name/value pair in query string. Additionally the function returns an integer with the number of elements in the result list.



    Plese Note, that if this function is called more than once any previous list sent as a parameter, will be destroyed. Lastly the "Resume Next" make sure the function keeps running and returns as much values, in the list, as possible.


    Also I came across this version of the similar program but in Java:


    public static java.util.Hashtable parseQueryString(String queryString) {
    StringTokenizer tokens = new StringTokenizer(queryString, "&");
    Hashtable params = new Hashtable();

    while (tokens.hasMoreTokens()){
    String token = tokens.nextToken();
    int equalIdx = token.indexOf('=');
    if (equalIdx != -1 && !token.equalsIgnoreCase("OpenAgent")){
    String name = token.substring(0, equalIdx);
    String value = token.substring(equalIdx + 1);
    params.put(name, value);
    }
    }
    return params;
    }



    The java version use a StringTokenizer to parse the url and returns a HashTable. The way you invoke this function in java would be:


    String qs= doc.getItemValueString("Query_String");
    java.util.Hashtable ht = parseQueryString(qs);
    String strUNID = (String) ht.get( "ID" );



    Please Note you have to cast the value to String. The link where you can find a sample of this code is:

    http://searchdomino.techtarget.com/tip/0,289483,sid4_gci1264609,00.html

    In my next post will show you want way to redirect a request using a simple print in Lotus Script.

     

    Comments

    1) Parameters with multiple =
    Tommy Valand | 11/6/2008 1:59:09 PM ET

    There is a potential flaw in your routine. It doesn't fetch the entire value if there are = inside a parameter-value.

    E.g. { Link }

    Safer way:

    'Split the querystring on "&" -> parameters

    Forall parameter in parameters

    name = Strtoken( parameter, "=", 1 )

    params( name ) = Replace( parameter, name+"=", "" )

    End Forall

    2) Quesy String Parser V2.0
    Ernest Leyva | 3/18/2009 2:04:22 PM ET

    Hi

    Thanks for the comment. This is the fixed version of the code. It also changed the parameters to make it slightly easier to invoke:

    Function parseQS( session as NotesSession, params List As String ) As Integer

    Dim buffer As Variant, pair As Variant, ceiling As Integer, i As Integer

    On Error Resume Next

    Erase params

    buffer = Split( session.DocumentContext.Query_String(0) , "&" )

    ceiling = Ubound( buffer )

    For i=0 To ceiling

    pair= buffer(i)

    params( Lcase( strToken( pair, "=", 1, 5 ) ) ) = strToken( pair, "=", 2, 5 )

    Next

    parseQS = ceiling

    End Function