I am required to pass an arithmetic operation like 2+8 to a restful back-end and receive the result. I know that a simple operation can be handled in frontend using javascript, but i just want to follow the requirement.
I send the operations with the following uri:
http://localhost:8080/?question=2+5
and in the back-end i have:
#RequestMapping("/")
public String getAnswer(#RequestParam("question") String question){
System.out.println("recieved question is: "+question);
return botService.Evaluator(question);
}
When i print the question it is like 2 3 so there is no operation there.
And the component complains with:
javax.script.ScriptException: <eval>:1:2 Expected ; but found 5
2 5
^ in <eval> at line number 1 at column number 2
So, why the + is missing?
and how can i fix it?
Use the URLEncoder class to make sure any special characters are encoded safely for transport.
You need to encode the '+' symbol as the server will just translate it as a space.
Send...
http://localhost:8080/?question=2%2B5
You need URL Encoding here. for more details regarding URL Encoding please have a look in below link.
URL Encoding
Related
I have a request as follows:
localhost:8000/location/:01
My code takes as input an HttpContext request.
func(HttpExchange r) {
String area_path = r.getRequestURI(); // Equals string "/location/"
}
How do I parse an HttpExchange correctly so I can pull out the "01" from this path and store it as a variable?
That (localhost:8000/location/:01) is not a valid URL or URI
A plain colon character is not legal in the path of a URL or URI. If you want to put a colon in the path, it must be percent-encoded. Furthermore, if this was a URL, it would start with a protocol; e.g. http:.
Now ... it is unclear what the HTTP stack you are using will do with a syntactically incorrect URL / URI, but it could simply be ignoring the colon and the characters after it.
Your code looks a bit odd too. You have tagged the question as [java]. But the code looks like JavaScript rather than Java; i.e. func is a Javascript keyword. But it also looks like you are using the (deprecated) com.sun.net.httpserver.HttpExchange Java class. I don't know what to make of that ...
My advice:
Don't use a colon character in the URL path.
If you must do it, then percent-encode the colon it.
If you cannot encode it properly, then you may need to find and use a different framework for your HTTP request handling. One that will accept and handle a malformed URL / URI in the way that you want. (Good luck finding one!)
Unfortunately, the details in your question are too sketchy to give more detailed advice.
I have this URI:
http://IP5:port/notification/myServlet.do?method=myMethod¶m1=[08]&method2=anotherParam¶m0=[07,04,06]
I want to obtain the URI's last part in the same order:
method=myMethod¶m1=[08]&method2=anotherParam¶m0=[07,04,06]
But' I can't discover how:
I can see in the request the field input, in order to do a substring after of ? character...
the methods: request.getRequestURI() and request.getRequestURL() is not working for me.
It looks like request is an HttpServletRequest. If so, you should be able to get what you are looking for using request.getQueryString().
I pass some parameters in ajax URL and want to get that parameters by request.getParameter(); in controller if that parameters have some special character like #,%,&, etc. then how to get it?
String xyz = new String(request.getParameter("XYZ").getBytes("iso-8859-1"), "UTF-8");
You have two options:
1.Encode values to JSON before sending, and decode them on server.
Use javascript method encodeURIComponent https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
I found best solution after spending couple of hours use
((String[])request.getParameterMap().get("paramname"))[0]
which gives me param value with special charater
The request parameter is like decrypt?param=5FHjiSJ6NOTmi7/+2tnnkQ==.
In the servlet, when I try to print the parameter by String param = request.getParameter("param"); I get 5FHjiSJ6NOTmi7/ 2tnnkQ==. It turns the character + into a space. How can I keep the orginal paramter or how can I properly handle the character +.
Besides, what else characters should I handle?
You have two choices
URL encode the parameter
If you have control over the generation of the URL you should choose this. If not...
Manually retrieve the parameter
If you can't change how the URL is generated (above) then you can manually retrieve the raw URL. Certain methods decode parameters for you. getParameter is one of them. On the other hand, getQueryString does not decode the String. If you have only a few parameters it shouldn't be difficult to parse the value yourself.
request.getQueryString();
//?param=5FHjiSJ6NOTmi7/+2tnnkQ==
If you want to use the '+' character in a URL you need to encode it when it is generated. For '+' the correct encoding is %2b
Use URLEncoder,URLDecoder's static methods for encoding and decoding URLs.
For example : -
Encode the URL param using
URLEncoder.encode(url,"UTF-8")
Back in the server side , decode this parameter using
URLDecoder.decode(url,"UTF-8")
decode method returns a String type of the decoded URL.
Allthough the question is some years old, I'd like to write down how I fixed the problem in my case: the download link to a file is created in a GWT page where
com.google.gwt.http.client.URL.encode(finalurl)
is used to encode the URL.
The problem was that the "+" sign a customer of us had in the filename wasn't encoded/escaped. So I had to remove the URL.encode(finalurl) and encode each parameter in the url with
URL.encodePathSegment(fileName)
I know my question is bound to GWT but it seems, URLEncoder.encode(string, encoding) should be applied to the parameter only aswell.
I am having a problem here. When I use ajax to pass a parameter containing "+" to my controller it is being replaced by a space.
Example, I will pass value = Tom+Jerry+Garfield using ajax. When I use System.out.println() in my controller it displays Tom Jerry Garfield. I tried using other special characters I don't seem to have a problem.
Please help. Thanks in advance.
In some GET and POST requests (most likely in the URL, or via a form), spaces are encoded as "+" (plus) symbols before they are passed to the server. You can see this behaviour if you do a normal GET request - you will see something like google.com?q=test+example If you want to pass a plus symbol via an ajax GET/POST request, you need to "urlencode" it. The URL encoded value for + is %2B.
Also note:
The javascript encodeURIComponent() function can be used, as answered in:
AJAX POST and Plus Sign ( + ) -- How to Encode?
+ is decoded as space after url decoding. If you want to pass +, you need to encode it.
When we pass values to the controller there is a model binder which is sitting in between the request. When the ajax call is made the url and the request is encoded. The " " (Space) character in url decoded form encodes to a "+".
The Model Binder on the other hand decodes the request and extracts the parameters and gives it to the controller and hence "+" is converted to a " " .
But here the question is why would one pass "+" as a separator ??