How to parse and extract parameters from a given url in Java

Last Updated on :March 2, 2024

To parse and extract parameters from a given URL in Java , you can use java.net.URL class.
In this article , we’ll explore two ways to extract parameters from a given URL in Java – using regular expressions (regex) , and using the split() method

1. Using regular expressions (regex)


package devtechinfo;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class URLParser {

    public static void main(String[] args) {
        URL url = null;
        String urlString = "http://www.abc.com/page?studentId=1001&StudentName=Rakesh";

        try {
            url = new URL(urlString);
            String queryParam = url.getQuery();

            Pattern pattern = Pattern.compile("studentId=([^&]+)");
            Matcher matcher = pattern.matcher(queryParam);

            if (matcher.find()) {
                System.out.println(matcher.group(1));
            }
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

    }
}

Output :


1001

In this example, we first create a URL object from the given URL string. Then we use the getQuery() method of the URL class. Then we define a regex pattern to match the query parameters in the URL string. The regex pattern “studentId=([^&]+)” matches the studentId followed by any number of characters, which is captured in a group. The Pattern and Matcher classes are used to apply the regex pattern to the URL string and find the first match.

If a match is found, we extract the parameters string from the first group using the group(1) method of the Matcher class.

2. Using the split() method

Here is another example where we can extract parameters from a given URL using String split() method

package devtechinfo;

import java.net.MalformedURLException;
import java.net.URL;

public class URLParser {

    public static void main(String[] args) {
        URL url = null;
        String urlString = "http://www.abc.com/page?studentId=1001&StudentName=Rakesh";

        try {
            url = new URL(urlString);
            String queryParam = url.getQuery();

            String[] paramArray = queryParam.split("&");
            for (String param : paramArray) {
                String[] keyValue = param.split("=");
                String key = keyValue[0];
                String value = keyValue[1];
                System.out.println("Parameter: " + key + " Value: " + value);
            }

        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }

    }
}

Output :


Parameter: studentId Value: 1001
Parameter: StudentName Value: Rakesh

In this example, we split the params string into an array of parameter strings using the split() method with the “&” delimiter. We then iterate through the array of parameter strings and split each one into key-value pairs using the split() method with the “=” delimiter. Finally, we print out each parameter key-value pair to the console.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *