0

I got the url value using request.getHeader("Referer") e.g.:

string rr=request.getHeader("Referer");
<%= rr %>

I got the url as http://www.sun.com/questions?uid=21&value=gg

Now I stored that url in as string, how do I get the value parameter value as uid=21 and value=gg

0

4 Answers 4

4

You need to:

  1. take the string after the '?'
  2. split that on '&' (you can do this and the above by using the URL object and calling getQuery())
  3. You'll then have strings of the form 'x=y'. Split on the first '='
  4. URLDecode the result parameter values.

This is all a bit messy, unfortunately.

Why the URLDecode step ? Because the URL will be encoded such that '=' and '?' in parameter values won't confuse a parser.

Sign up to request clarification or add additional context in comments.

2 Comments

getParameter will yield results from the current URL, not from the REFERER URL.
Doh. Well spotted and changed approriately
3

This tutorial might help a bit.

What you need to do is parse the URL, then get the 'query' property, then parse it into name/value pairs.

So something like this:

URL url = new URL(referer);
String queryStr = url.getQuery();

String[] params = queryStr.split("&");
for (String param: params) {
    String key = param.substring(0, param.indexOf('=');
    String val = param.substring(param.indexOf('=') + 1);
}

Disclaimer: this has not been tested, and you will need to do more error checking!

3 Comments

Good catch - you'd probably need to decode using URLDecoder.decode(...)
As for the error checking: not each parameter needs a value. (Which is not really an error, but needs to be taken care of as then the '=' may be missing.)
@Phill Sacre - it is usually preferable to use the URI class in preference to URL.
0

Below an example how Apache Solr does it (SolrRequestParsers).

  /**
   * Given a standard query string map it into solr params
   */
  public static MultiMapSolrParams parseQueryString(String queryString) 
  {
    Map<String,String[]> map = new HashMap<String, String[]>();
    if( queryString != null && queryString.length() > 0 ) {
      try {
        for( String kv : queryString.split( "&" ) ) {
          int idx = kv.indexOf( '=' );
          if( idx > 0 ) {
            String name = URLDecoder.decode( kv.substring( 0, idx ), "UTF-8");
            String value = URLDecoder.decode( kv.substring( idx+1 ), "UTF-8");
            MultiMapSolrParams.addParam( name, value, map );
          }
          else {
            String name = URLDecoder.decode( kv, "UTF-8" );
            MultiMapSolrParams.addParam( name, "", map );
          }
        }
      }
      catch( UnsupportedEncodingException uex ) {
        throw new SolrException( SolrException.ErrorCode.SERVER_ERROR, uex );
      }
    }
    return new MultiMapSolrParams( map );
  }

Usage:

MultiMapSolrParams params = SolrRequestParsers.parseQueryString( req.getQueryString() );

And an example how Jersey does it (UriComponent):

/**
 * Decode the query component of a URI.
 * 
 * @param q the query component in encoded form.
 * @param decode true of the query parameters of the query component
 *        should be in decoded form.
 * @return the multivalued map of query parameters.
 */
public static MultivaluedMap<String, String> decodeQuery(String q, boolean decode) {
    MultivaluedMap<String, String> queryParameters = new MultivaluedMapImpl();

    if (q == null || q.length() == 0) {
        return queryParameters;
    }

    int s = 0, e = 0;
    do {
        e = q.indexOf('&', s);

        if (e == -1) {
            decodeQueryParam(queryParameters, q.substring(s), decode);
        } else if (e > s) {
            decodeQueryParam(queryParameters, q.substring(s, e), decode);
        }
        s = e + 1;
    } while (s > 0 && s < q.length());

    return queryParameters;
}

private static void decodeQueryParam(MultivaluedMap<String, String> params,
        String param, boolean decode) {
    try {
        int equals = param.indexOf('=');
        if (equals > 0) {
            params.add(
                    URLDecoder.decode(param.substring(0, equals), "UTF-8"),
                    (decode) ? URLDecoder.decode(param.substring(equals + 1), "UTF-8") : param.substring(equals + 1));
        } else if (equals == 0) {
            // no key declared, ignore
        } else if (param.length() > 0) {
            params.add(
                    URLDecoder.decode(param, "UTF-8"),
                    "");
        }
    } catch (UnsupportedEncodingException ex) {
        // This should never occur
        throw new IllegalArgumentException(ex);
    }
}

Comments

0

I combine two codes and result is below:

StringBuffer url = request.getRequestURL(); 
    if(request.getQueryString()!= null){    
    url.append("?"); 
    url.append(request.getQueryString()); 
    } 
    String url_param = url.toString();          
    String[] params = url_param.split("&");

    String[][] param_key = new String[1320][3];
    String[][] param_val = new String[1320][3];
    String param = "";
    String key_tmp = "";
    String val_tmp = "";
    y = 1;

    for(x=1;x<params.length;x++)
    {
        param = params[x];
        key_tmp = param.substring(0, param.indexOf("="));
        param_key[x][y] = key_tmp;
        y++;
        val_tmp = param.substring(param.indexOf("=") + 1);
        param_val[x][y] = val_tmp;
        y=1;
    }

You get two arrays. One contains key(name), second contains values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.