This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

How can I parse url query string parameter in java?

 
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.
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
see here
 
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 ]
 
(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.
 
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.
 
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.)
 
Marshal
Posts: 67343
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!
 
Looky! I'm being abducted by space aliens! Me and this tiny ad!
Sauce Labs - World's Largest Continuous Testing Cloud for Websites and Mobile Apps
https://coderanch.com/t/722574/Sauce-Labs-World-Largest-Continuous
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!