• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

How can I parse url query string parameter in java?

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
If I have a URL string like this:
http://www.javaranch.com

How can I parse the query string parameter?
e.g. parameter 'ubb' is nettopic
parmater 'f' is 1

From the java doc, it only gives me the query string of the url:
http://java.sun.com/j2se/1.4.2/docs/api/java/net/URL.html

Thank you.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
see here
 
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The easiest way is probably to use the getQuery() from URL class and then parse the result returned by the getQuery(). For example:

To iterate the map, you can do this:

[ August 07, 2007: Message edited by: Freddy Wong ]
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was a bit surprised the other day when I did getParameterMap().entrySet() from a J2EE Request object, the value in each entry is an array. That supports having the same key more than once in the query string. Think about whether you want to handle that.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Using request.getParameter("ubb") is significantly easier than iterating through the entire query string.

If, however, you have multiple possible values (which the query string supports, though rarely used) you would have to iterate through.

And the value would be newtopic, not nettopic.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
(Not sure that it's "rarely used"; I see it all the time for checkboxes, multiple selects, etc.)
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Joe Donahue wrote:If, however, you have multiple possible values (which the query string supports, though rarely used) you would have to iterate through.


Not correct on both counts. It is very common to have multiple parameters with the same name, and the getParameterValues() method gives them to you as an array without the need for dealing with the query string.
 
Joe Donahue
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well played.

BTW - I made your frappacinos. They are fantastic!
 
reply
    Bookmark Topic Watch Topic
  • New Topic