Java URL Query Parse parseURLQuery(String query)

Here you can find the source of parseURLQuery(String query)

Description

Parse an URL/URI query string consisting of attribute=value pairs separated by ampersands, into a map of the attribute name mapped to their values.

License

Open Source License

Exception

Parameter Description
UnsupportedEncodingException an exception

Declaration

public static Map<String, String> parseURLQuery(String query) throws UnsupportedEncodingException 

Method Source Code

//package com.java2s;
/*/* w ww.  j av a2 s  .c  om*/
 * Copyright 2007-2008 Sun Microsystems, Inc. All Rights Reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
 *
 * This code is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2
 * only, as published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License version 2 for more details (a copy is
 * included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU General Public License
 * version 2 along with this work; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA
 *
 * Please contact Sun Microsystems, Inc., 16 Network Circle, Menlo
 * Park, CA 94025 or visit www.sun.com if you need additional
 * information or have any questions.
 */

import java.io.UnsupportedEncodingException;

import java.net.URLDecoder;

import java.util.HashMap;

import java.util.Map;

public class Main {
    /**
     * Parse an URL/URI query string consisting of attribute=value pairs separated by ampersands, into a map of the attribute name mapped to their values.
     * The presence of an attribute with no value, maps to the empty string.
     * 
     * @throws UnsupportedEncodingException
     */
    public static Map<String, String> parseURLQuery(String query) throws UnsupportedEncodingException {
        if (query == null)
            return null;

        Map<String, String> result = new HashMap<String, String>();
        if (query != null) {
            query = URLDecoder.decode(query, "8859_1");
            String[] st = query.split("&");

            for (int i = 0; i < st.length; i++) {
                String[] av = st[i].split("=", 2);
                String key = av[0];
                String value = (av.length > 1) ? av[1] : "";
                result.put(key, value);
            }
        }
        return result;
    }
}

Related

  1. parseQueryString(String queryString)
  2. parseQueryString(String queryString)
  3. parseQuerystring(String queryString)
  4. parseQueryString(String queryString, Map params)
  5. parseQueryStringEx(String queryString)