The code below is shows how to extract a parameter from a
URL string. This is useful when you need to extract a parameter value from the
HTTP referrer string, which itself is a parameter of the HttpServletRequest
object.
String searchTerm = null;
String urlString = "http://www.somewebaddress.co.uk/?test=blah+blah+blah&Test2=stuff";
URL url;
try {
url = new URL(urlString);
String queryString = url.getQuery();
//Check for single or multiple params
String[] params = (queryString.split("&").length > 0) ? queryString.split("&"): new String[]{queryString};
for(String param : params){
String[] paramNameValuePair = param.split("=");
if(paramNameValuePair[0].equals("test")){
searchTerm = paramNameValuePair[1];
System.out.println(searchTerm);
}
}
} catch (MalformedURLException e) {
System.out.println("Invalid URL!!");
}
No comments:
Post a Comment
Note: only a member of this blog may post a comment.