Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass multiple parameter in URL?

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?

URL url; double lat=touchedPoint.getLatitudeE6() / 1E6; double lon=touchedPoint.getLongitudeE6() / 1E6; url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+lon); 

In this case output (written to file) is 28.53438677.472097. This is working but I want to pass latitude and longitude in two separate parameters so that my work at server side is reduced. If it is not possible how can I at least add a space between lat & lon so that I can use tokenizer class to get my latitude and longitude. I tried following line but to no avail.

    url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+" "+lon); output- Nothing is written to file         url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"&?param2="+lon); output- 28.534386 (Only Latitude)         url = new URL("http://10.0.2.2:8080/HelloServlet/PDRS?param1="+lat+"?param2="+lon); output- 28.532577?param2=77.502996 

My servlet code is as follows:

req.setCharacterEncoding("UTF-8"); resp.setCharacterEncoding("UTF-8"); final String par1 =  req.getParameter("param1"); final String par2 = req.getParameter("param2"); FileWriter fstream = new FileWriter("C:\\Users\\Hitchhiker\\Desktop\\out2.txt"); BufferedWriter out = new BufferedWriter(fstream); out.write(par1); out.append(par2); out.close(); 

Also I wanted to the know is this the most safe and secured way to pass the data from android device to server.

like image 295
rishiag Avatar asked Jun 08 '12 05:06

rishiag


People also ask

How do I pass multiple parameters in URL in Postman?

In this parameter, i.e., is the key and, UTF-8 is the key-value. Enter the same URL in the Postman text field; you will get the multiple parameters in the Params tab. Even you can write each of the parameters and send a request with multiple parameters.

Can there be multiple in a URL?

What triggers this issue? There are internal URLs in the web resource that contain multiple ampersands (“&”) and, consequently, multiple parameters.


Video Answer


1 Answers

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.

like image 60
maaartinus Avatar answered Sep 22 '22 18:09

maaartinus