print Parameters from URL - Java java.net

Java examples for java.net:URL Query

Description

print Parameters from URL

Demo Code

//package com.java2s;

import java.net.URL;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

public class Main {
    public static void printParameters(URL url) {
        String query = url.getQuery();
        Map<String, String> map = getQueryMap(query);
        Set<String> keys = map.keySet();
        for (String key : keys) {
            System.out.println("Name=" + key);
            System.out.println("Value=" + map.get(key));
        }// w w w  .  jav  a  2s.c o m
    }

    public static Map<String, String> getQueryMap(String query) {
        String[] params = query.split("&");
        Map<String, String> map = new LinkedHashMap<String, String>();
        for (String param : params) {
            String name = param.split("=")[0];
            String value = param.split("=")[1];
            map.put(name, value);
        }
        return map;
    }
}

Related Tutorials