Solved

parsing url query params

Posted on 2010-09-22
4
426 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

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…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

828 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