How do I pass multiple parameter in URL?

前端 未结 3 1301
猫巷女王i
猫巷女王i 2020-12-13 00:38

I am trying to figure out how to pass multiple parameters in a URL. I want to pass latitude and longitude from my android class to a java servlet. How can I do that?

相关标签:
3条回答
  • 2020-12-13 01:02

    I do not know much about Java but URL query arguments should be separated by "&", not "?"

    http://tools.ietf.org/html/rfc3986 is good place for reference using "sub-delim" as keyword. http://en.wikipedia.org/wiki/Query_string is another good source.

    0 讨论(0)
  • 2020-12-13 01:03

    This

    url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&param2="+lon);
    

    must work. For whatever strange reason1, you need ? before the first parameter and & before the following ones.

    Using a compound parameter like

    url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"_"+lon);
    

    would work, too, but is surely not nice. You can't use a space there as it's prohibited in an URL, but you could encode it as %20 or + (but this is even worse style).


    1 Stating that ? separates the path and the parameters and that & separates parameters from each other does not explain anything about the reason. Some RFC says "use ? there and & there", but I can't see why they didn't choose the same character.

    0 讨论(0)
  • 2020-12-13 01:06

    You can pass multiple parameters as "?param1=value1&param2=value2"

    But it's not secure. It's vulnerable to Cross Site Scripting (XSS) Attack.

    Your parameter can be simply replaced with a script.

    Have a look at this article and article

    You can make it secure by using API of StringEscapeUtils

    static String   escapeHtml(String str) 
              Escapes the characters in a String using HTML entities.
    

    Even using https url for security without above precautions is not a good practice.

    Have a look at related SE question:

    Is URLEncoder.encode(string, "UTF-8") a poor validation?

    0 讨论(0)
提交回复
热议问题