You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by David Wall <d....@computer.org> on 2008/08/06 01:17:05 UTC

Parsing a URL to see if a param exists

Is there an API call to parse an URL I have as a String so that I can 
determine if a given param exists? 

I know that when processing from Tomcat, I can do 
request.getParameterValues() for the URL of my servlet, but I'd like to 
know if the URL used to arrive at my page includes a given parameter on 
the URL itself (and not from a POST hidden param that will also be 
returned by getParameterValues).

Thanks,
David

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Parsing a URL to see if a param exists

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Johnny,

Johnny Kewl wrote:
| Dave this is how I do it...
|
|            Enumeration paramNames = request.getParameterNames();

Go back and read the OP: he says that he's got a String and he wants to
treat it as a URL and check for parameters. He's not looking for
parameters in the current request.

|            while(paramNames.hasMoreElements()) {
|                paramName = (String)paramNames.nextElement();
|                if(paramName.compareToIgnoreCase("save") == 0){
|                    //Do something useful
|                    paramValue = request.getParameter(paramName);
|                }                           }

This implementation is wrong, because URL parameters are case-sensitive.
It's also stupid, since the easier implementation would be:

boolean paramIsDefined = null != request.getParameter(paramName);

:(

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkiZ29wACgkQ9CaO5/Lv0PCIzQCdEgtZeMJ5p1N/gKOgSZy3HJ0x
A/gAoKRZKViJyHRckdFJdKiYFMgABg19
=WlJR
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Parsing a URL to see if a param exists

Posted by Johnny Kewl <jo...@kewlstuff.co.za>.
----- Original Message ----- 
From: "David Wall" <d....@computer.org>
To: "Tomcat Users List" <us...@tomcat.apache.org>
Sent: Wednesday, August 06, 2008 1:17 AM
Subject: Parsing a URL to see if a param exists


> Is there an API call to parse an URL I have as a String so that I can 
> determine if a given param exists? 
> 
> I know that when processing from Tomcat, I can do 
> request.getParameterValues() for the URL of my servlet, but I'd like to 
> know if the URL used to arrive at my page includes a given parameter on 
> the URL itself (and not from a POST hidden param that will also be 
> returned by getParameterValues).
> 
> Thanks,
> David

Dave this is how I do it...

            Enumeration paramNames = request.getParameterNames();
            while(paramNames.hasMoreElements()) {
                paramName = (String)paramNames.nextElement();
                if(paramName.compareToIgnoreCase("save") == 0){
                    //Do something useful
                    paramValue = request.getParameter(paramName);
                }                
            }

Maybe?
Its probably all pre parsed by TC already...

---------------------------------------------------------------------------
HARBOR : http://www.kewlstuff.co.za/index.htm
The most powerful application server on earth.
The only real POJO Application Server.
See it in Action : http://www.kewlstuff.co.za/cd_tut_swf/whatisejb1.htm
---------------------------------------------------------------------------

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Parsing a URL to see if a param exists

Posted by David Wall <d....@computer.org>.
Thanks for the code idea.  I like what you wrote.
>
> If there's no '?' in the URL, then you can return false right away, 
> right?
Indeed!

> public boolean isParamInUrl(String url, String paramName)
> {
> ~   return url.contains('?' + paramName + '=')
> ~       || url.contains('&' + paramName + '=');
> }
>
> This looks simpler, easier to read, and less error-prone. I would
> venture a guess that it runs faster than your implementation, too. 
Yes, I think I like it.  I think I have to save a 
URLEncoder.encode(paramName) first and then use that in the to 
url.contains calls just to be safe.  Your code is much easier to 
understand and no doubt runs faster.

Thanks,
David

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Parsing a URL to see if a param exists

Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

David,

You could use HttpUtils.parseQueryString, but the entire class is
deprecated. I like your home made parser below, with a couple of tweaks:

David Wall wrote:
| Not really since I just need to process the query string portion to see
| if a given param exists or not.  I'm using a simple homegrown parser now:
|
|    public boolean isParamInUrl(String url, String paramName)
|    {
|        int pos = url.indexOf("?");
|        if ( pos > 0 )
|            url = url.substring(pos);

If there's no '?' in the URL, then you can return false right away, right?

|        String[] paramValues = url.split("&");

This uses a regular expression which is relatively slow since you
already know what you're looking for.

How about this implementation:

public boolean isParamInUrl(String url, String paramName)
{
~   return url.contains('?' + paramName + '=')
~       || url.contains('&' + paramName + '=');
}

This looks simpler, easier to read, and less error-prone. I would
venture a guess that it runs faster than your implementation, too. I
could increase the speed a bit by using StringBuilder directly instead
of allowing the compiler to write code for me, but that would make it
more difficult to read.

Enjoy,
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkiZy80ACgkQ9CaO5/Lv0PAHTACeOuvK34ABWpi0BWGWxs9ycrpL
PXcAoLINzsN1rVXgXG0fR+rI0M4TUk8U
=/+RK
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Parsing a URL to see if a param exists

Posted by David Wall <d....@computer.org>.
>>         if ( pos > 0 )
>>             url = url.substring(pos);
>>     
Found a bug in that this should be pos+1 to remove the "?".

>>             if ( paramAndValue[0].equals(paramName) )
>>                 return true;
>>     
We're also doing a URLDecoder.decode on the paramAndValue[0] in case the 
param name was encoded (though in practice it's rarely done).


> Wouldn't be faster/less coding if you just use this?
>
> URL aURL = new URL(url);
> String urlParams = aURL.getQuery();
> if (urlParams.contains(paramName)) {
> // proccess as needed if paramName exists
> } else {
> // process as otherwise
> }
>   
That would find the paramName even if was a substring of a param name or 
the param value.  That is, if I'm checking if param "name" exists, and 
the URL query string was "firstname=bob" or "sort=nameOrder" that code 
would say "name" exists but no such "name" param is there.

David


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Parsing a URL to see if a param exists

Posted by Tommy Pham <to...@yahoo.com>.
--- On Tue, 8/5/08, David Wall <d....@computer.org> wrote:

> From: David Wall <d....@computer.org>
> Subject: Re: Parsing a URL to see if a param exists
> To: "Tomcat Users List" <us...@tomcat.apache.org>
> Date: Tuesday, August 5, 2008, 7:50 PM
> >
> http://java.sun.com/docs/books/tutorial/networking/urls/urlInfo.html
> >
> > It breaks down every component of the url (protocol,
> domain name, etc).  Is that what you're looking for?
> >   
> 
> Not really since I just need to process the query string
> portion to see 
> if a given param exists or not.  I'm using a simple
> homegrown parser now:
> 
>     public boolean isParamInUrl(String url, String
> paramName)
>     {
>         int pos = url.indexOf("?");
>         if ( pos > 0 )
>             url = url.substring(pos);
>         String[] paramValues =
> url.split("&");
>         if ( paramValues == null )
>             return false;
>        
>         for( String paramValue : paramValues )
>         {
>             String[] paramAndValue =
> paramValue.split("=");
>             if ( paramAndValue == null ||
> paramAndValue.length != 2 )
>                 return false;
>             if ( paramAndValue[0].equals(paramName) )
>                 return true;
>         }
>         return false;
>     }
> 
> The concern is whether this is valid should the URL have
> complex params 
> and such based on encoding schemes.  I mean, it just splits
> the url 
> query string into an array of param-values '&',
> and then splits that by 
> '=' without concern for any encoding.
> 
> David
> 
> 
Hi,

Wouldn't be faster/less coding if you just use this?

URL aURL = new URL(url);
String urlParams = aURL.getQuery();
if (urlParams.contains(paramName)) {
// proccess as needed if paramName exists
} else {
// process as otherwise
}

just a thought :)

---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Parsing a URL to see if a param exists

Posted by David Wall <d....@computer.org>.
> http://java.sun.com/docs/books/tutorial/networking/urls/urlInfo.html
>
> It breaks down every component of the url (protocol, domain name, etc).  Is that what you're looking for?
>   

Not really since I just need to process the query string portion to see 
if a given param exists or not.  I'm using a simple homegrown parser now:

    public boolean isParamInUrl(String url, String paramName)
    {
        int pos = url.indexOf("?");
        if ( pos > 0 )
            url = url.substring(pos);
        String[] paramValues = url.split("&");
        if ( paramValues == null )
            return false;
       
        for( String paramValue : paramValues )
        {
            String[] paramAndValue = paramValue.split("=");
            if ( paramAndValue == null || paramAndValue.length != 2 )
                return false;
            if ( paramAndValue[0].equals(paramName) )
                return true;
        }
        return false;
    }

The concern is whether this is valid should the URL have complex params 
and such based on encoding schemes.  I mean, it just splits the url 
query string into an array of param-values '&', and then splits that by 
'=' without concern for any encoding.

David



---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: Parsing a URL to see if a param exists

Posted by Tommy Pham <to...@yahoo.com>.
--- On Tue, 8/5/08, David Wall <d....@computer.org> wrote:

> From: David Wall <d....@computer.org>
> Subject: Parsing a URL to see if a param exists
> To: "Tomcat Users List" <us...@tomcat.apache.org>
> Date: Tuesday, August 5, 2008, 7:17 PM
> Is there an API call to parse an URL I have as a String so
> that I can 
> determine if a given param exists? 
> 

http://java.sun.com/docs/books/tutorial/networking/urls/urlInfo.html

It breaks down every component of the url (protocol, domain name, etc).  Is that what you're looking for?

> I know that when processing from Tomcat, I can do 
> request.getParameterValues() for the URL of my servlet, but
> I'd like to 
> know if the URL used to arrive at my page includes a given
> parameter on 
> the URL itself (and not from a POST hidden param that will
> also be 
> returned by getParameterValues).
> 
> Thanks,
> David
> 


---------------------------------------------------------------------
To start a new topic, e-mail: users@tomcat.apache.org
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org