This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Functional Reactive Programming and have Stephen Blackheath and Anthony Jones on-line!
See this thread for details.
Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Query String Methods

 
Paul Wetzel
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
 
paul wheaton
Trailboss
Pie
Posts: 21752
Firefox Browser IntelliJ IDE 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
 
Frank Carver
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic