pasring a queryString

Hi,
I would like to parse a query String below into a map, is there any java api method i can use to parse the string? I am doing the parsing in my Servlet Class.
The String:
"firstName=Raoul&latName=Brooks&birthDay=05/16/1980&IP=127.0.1&email=rBrooks@yahoo.com"
komlaaaAsked:
Who is Participating?
 
colr__Commented:
use String[] result = theMainString.split("&"); to extract the individual name=value pairts, then use String[] result = theBitString.split("="); on each of the individual elements to parse the name and value.

colr__
0
 
fargoCommented:
There is no direct way, but may be u can use the following.

    /**
     * Parses the query and string the parameters in the hashmap
     */
    private void parseQuery(String query)
    {
        // check if query exists
        if (query == null || query.length() == 0)
            return;

        // creating a tokenizer
        StringTokenizer tokenizer = new StringTokenizer(query, "&");

        // iterate over tokens
        while (tokenizer.hasMoreTokens())
        {
            // getting the next parameter
            String parameter = tokenizer.nextToken();

            // getting delimiter index
            int equalIdx = parameter.indexOf("=");

            // get name
            String name = parameter.substring(0, equalIdx);

            // get value
            String value = parameter.substring(equalIdx + 1);

            // add new parameter
            hashMap.put(name,value);
        }
    }

init the hashmap in your class.

0
 
fargoCommented:
ah..colr__ sorry...i also answered the same...the only difference is my method.

didn't refresh it on time.
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
rrzCommented:
I don't know if you will find this easier but here is another way.  
  Map paramMap = request.getParameterMap();  
But getting the values out is more work because the values are stored as String arrays.  
  String firstName = ((String[])paramMap.get("firstName"))[0];
  String lastName = ((String[])paramMap.get("lastName"))[0];
0
 
dear_sweet_udayCommented:
Hi
     you can use request.getParameterNames() Its of Enumeration type
 
   Code Below :>

  java.util.Enumeration e = request.getParameterNames();

   while (e.hasMoreElements())
  {

        String key = (String)e.nextElement();
        String[] values = request.getParameterValues(key);
   }

 Its simple to use
0
 
rrzCommented:
dear,  the question was  
>I would like to parse a query String below into a map
0
 
dear_sweet_udayCommented:
Yup
  but Enumeration gives you name & value both
  & Map requires Name & value pair ............so you can do that conversion
0
 
rrzCommented:
Yeah, your right.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.