• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 175
  • Last Modified:

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"
0
komlaaa
Asked:
komlaaa
  • 3
  • 2
  • 2
  • +1
4 Solutions
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now