Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting URL parameter in java and extract a specific text from that URL

I have a URL and I need to get the value of v from this URL. Here is my URL: http://www.youtube.com/watch?v=_RCIP6OrQrE

How can I do that?

like image 219
codezoner Avatar asked Jul 31 '12 05:07

codezoner


People also ask

How do you separate a variable in URL?

URL parameters are made of a key and a value, separated by an equal sign (=). Multiple parameters are each then separated by an ampersand (&).

What is used to extract the query parameters from the URL?

QueryParam annotation in the method parameter arguments. The following example (from the sparklines sample application) demonstrates using @QueryParam to extract query parameters from the Query component of the request URL.

How do you handle a parameter in a URL?

URL parameters should be added to a URL only when they have a function. Don't permit parameter keys to be added if the value is blank. In the above example, key2 and key3 add no value both literally and figuratively. Avoid applying multiple parameters with the same parameter name and a different value.


1 Answers

I think the one of the easiest ways out would be to parse the string returned by URL.getQuery() as

public static Map<String, String> getQueryMap(String query) {       String[] params = query.split("&");       Map<String, String> map = new HashMap<String, String>();      for (String param : params) {           String name = param.split("=")[0];           String value = param.split("=")[1];           map.put(name, value);       }       return map;   } 

You can use the map returned by this function to retrieve the value keying in the parameter name.

like image 186
verisimilitude Avatar answered Sep 28 '22 12:09

verisimilitude