Solved

parsing url query params

Posted on 2010-09-22
4
422 Views
Last Modified: 2013-12-29
What’s the most efficient way to do this in java:

I need a method that would clean specified parameters from a url and another method that would keep the specified parameters in the url and remove all the other parameters.  So the signatures would be something like this:

public static String getUrlWithoutTheFollowingParameters (String url,  List<String> queryParams) ()

public static String getUrlWithOnlyTheFollowingParameters( String url, List<String> queryParams)()

I want to do this w/o using any third-party library.

Thanks!
0
Comment
Question by:aturetsky
  • 2
4 Comments
 
LVL 10

Expert Comment

by:Hegemon
Comment Utility
You can use a filter servlet to call your methods. Once they are called, just iterate the list of actual parameters, compare with your black/white lists and remove accordingly.
0
 
LVL 40

Expert Comment

by:gurvinder372
Comment Utility
split the url as per '?'

so if the url is

String url = "http://www.experts-exchange.com/Programming/Languages/Java/J2SE/Q_26492591.html?abc1=new1&abc2=new2&abc3=new3&abc4=new4&"

String urlItems = url.split("\?");
String queryString = urlItems[ 1 ];

//now you can get the query params pair
String queryParamsPair = queryString.split("&");

//construct a hashmap
Map queryParamsMap = new HashMap();

for ( int counter = 0; counter < queryParamsPair.length; counter++  )
{
    String items = queryParamsPair[ counter ].split("=");
    queryParamsMap.put( items[ 0 ], items[ 1 ] )
}

now you have got the hashmap, you can easily do add, remove of the query params and construct the url again
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
How about this:
public static String getUrlWithoutTheFollowingParameters(String url, List<String> queryParams) {
	int i = url.indexOf("?") + 1;
	StringBuilder result = new StringBuilder(url.substring(0, i));

	for (int j = i; j < url.length(); j++) {
		if (url.charAt(j) == '=') {
			String key = url.substring(i, j);
			String param;

			while (j < url.length() && url.charAt(j) != '&') {
				j++;
			}

			param = url.substring(i, j);

			if (!queryParams.contains(key)) {
				result.append(url.substring(i, j));
				result.append('&');
			}

			i = j + 1;
		}
	}

	return result.toString().substring(0, result.length() - 1);
}

public static String getUrlWithOnlyTheFollowingParameters(String url, List<String> queryParams) {
	int i = url.indexOf("?") + 1;
	StringBuilder result = new StringBuilder(url.substring(0, i));

	for (int j = i; j < url.length(); j++) {
		if (url.charAt(j) == '=') {
			String key = url.substring(i, j);
			String param;

			while (j < url.length() && url.charAt(j) != '&') {
				j++;
			}

			param = url.substring(i, j);

			if (queryParams.contains(key)) {
				result.append(url.substring(i, j));
				result.append('&');
			}

			i = j + 1;
		}
	}

	return result.toString().substring(0, result.length() - 1);
}

Open in new window

0
 
LVL 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
Note, the only real difference between the two above is at the line:

    if (!queryParams.contains(key)) {

If you wanted, you could create an enum for a "mode" and then have one function:
public static String ScrubUrl(String url, List<String> queryParams, Mode scrubMode) {
	int i = url.indexOf("?") + 1;
	StringBuilder result = new StringBuilder(url.substring(0, i));

	for (int j = i; j < url.length(); j++) {
		if (url.charAt(j) == '=') {
			String key = url.substring(i, j);
			String param;

			while (j < url.length() && url.charAt(j) != '&') {
				j++;
			}

			param = url.substring(i, j);

			switch (scrubMode) {
				case KeepParams:
					if (queryParams.contains(key)) {
						result.append(url.substring(i, j));
						result.append('&');
					}
					break;
				case RemoveParams:
					if (!queryParams.contains(key)) {
						result.append(url.substring(i, j));
						result.append('&');
					}
					break;
			}

			i = j + 1;
		}
	}
	return result.toString().substring(0, result.length() - 1);
}

// Mode.java
public enum Mode
{
    KeepParams,
    RemoveParams,
}

Open in new window

0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Whatever be the reason, if you are working on web development side,  you will need day-today validation codes like email validation, date validation , IP address validation, phone validation on any of the edit page or say at the time of registration…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now