Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

parsing url query params

Posted on 2010-09-22
4
Medium Priority
?
430 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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:Gurvinder Pal Singh
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 2000 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

The top UI technologies you need to be aware of

An important part of the job as a front-end developer is to stay up to date and in contact with new tools, trends and workflows. That’s why you cannot miss this upcoming webinar to explore the latest trends in UI technologies!

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn different types of Android Layout and some basics of an Android App.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

721 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