3

Possible Duplicate:
Parsing query strings in Java

PHP has parse_str(), is there an equivalent function that does the same thing in java?

Community
  • 1
  • 1
Lumpy
  • 3,632
  • 4
  • 34
  • 58
  • It "names" given to some functions is quite baffling... –  May 17 '11 at 19:24
  • Your question appears to be answered by this question: http://stackoverflow.com/questions/1667278/parsing-query-strings-in-java – fredley May 17 '11 at 19:24
  • @pst On the other hand, Java's taking 30 lines to do what we PHPers do in one is also baffling. We all pick our poisons! – ceejayoz May 17 '11 at 19:30
  • 1
    I think I didn't read the documentation for `parse_str` closely enough - as @Havard S notes in his answer, there are two modes of operation. The question this was marked as a duplicate of points out how to achieve the second part - parsing the query string. AFAIK, the first mode, creating variables isn't possible in Java since it's not a dynamic language, unlike PHP. – no.good.at.coding May 17 '11 at 19:33
  • 1
    I understand both these questions have the same answer but I do not believe they are the same question. I tried to search for this before posting and could not find an answer. Does this question stay available to be searched even if the question is closed? – Lumpy May 17 '11 at 19:34
  • I agree with Lumpys remark. This is not a dupe, even if it ends up having the same practical answer. – Håvard S May 17 '11 at 19:36

2 Answers2

3

PHPs parse_str has two modes of operation:

  1. If given a single argument, parse_str will set parameters as variables in the current scope. For this mode of operation, there is no equivalent in Java.
  2. If given two arguments, the second argument must be an array which will be populated with the query string parameters/values. For an equivalent in Java, see this answer on parsing query strings in Java.
Community
  • 1
  • 1
Håvard S
  • 23,244
  • 8
  • 61
  • 72
  • 1
    +1 for clarifying what it is that `parse_str` actually does! AFAIK, you can't do the equivalent of #1 in Java since it's not a dynamic language. – no.good.at.coding May 17 '11 at 19:30
0

You can use the ServletRequest object's getParameter method. http://download.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html)

Lotus Notes
  • 6,302
  • 7
  • 32
  • 47