Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

How can I parse url query string parameter in java?

 
ying lam
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
  • 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.
 
Vijayender Bandaru
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see here
 
Freddy Wong
Ranch Hand
Posts: 959
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • 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 ]
 
Stan James
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
  • 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.
 
Joe Donahue
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(Not sure that it's "rarely used"; I see it all the time for checkboxes, multiple selects, etc.)
 
Bear Bibeault
Marshal
Posts: 67420
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
Well played.

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