Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring 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

Query String Methods

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a nice method in a request object that would load a query string into a hashtable or something similar? There seems to be similar functionality in the getParameterNames for form elements (text inputs etc.) but not for query string. Or am i just wrong? thanks in advance
paul
 
Trailboss
Posts: 23210
IntelliJ IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Query string? This smacks of SQL - is that what you mean?
 
Paul Wetzel
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry for the ambiguity. The query string that is passed in the url. That is, if you look at the address/location of this web page in your browser it is something like: "http://www.eporkchop.com/cgi-bin/cgiwrap/~banana/postings.cgi?action=repley&form=+++servlets...". well every thing past the ? is the query string where we have keys "action" and "form" and values "reply" and "servlets" where key action goes w/ reply... what i want is to get these keys and values into a hashtable or at least get an enumeration of the keys and then access them thru a request method much like that of parameters and header (getParameterValue(name)). thanks for your attention to this subject. paul
 
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The method ServletRequest.getParameterNames() returns an Enumeration of the names from both the URL query string and the post data if present. It's pretty easy to loop through this and access all the named parameters.
The reason that it's not available as a Hashtable is that it can be a one-to-many relationship between name and value. Consider the simple URL:

This is perfectly valid, but has two values for "a". Duplication like this can also appear in the POST data, so you need to be careful with your servlet design to make sure this is handled properly.
The best place to look for example code is in SnoopServlet.java, provided in the examples directory of most JSDK downloads. This extracts and displays just about everything it can from the request and the context.
 
Why fit in when you were born to stand out? - Seuss. Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic