Solved

parsing url query params

Posted on 2010-09-22
4
424 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
ID: 33738183
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
ID: 33738227
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 75

Expert Comment

by:käµfm³d 👽
ID: 33738551
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 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 33738701
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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
rhino JavaScript import, load 25 85
servlet requestdispatcher include and forward differences 1 32
hibernate example for saving data 19 43
jsp login check 12 32
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
The viewer will learn how to implement Singleton Design Pattern in Java.

821 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