How to send special characters in ajax request in EXTJS - java

I have simple extjs(2.0) form and it contains dynamically generated textfields. I am sending the form parameters to servlet using ajax request. If i enter any special characters in textfield, the textfield values are coming as URL escape codes.
For Example & is represented with %26 in servlet.
Instead of using ajax request if i use form submit, it is working. Please help me to solve this issue.

Encode values to JSON before sending, and decode them on server.

If you want to send the parameters to 'GET' method then use url encoding and send.
Try looking into encodeURI() and decodeURI().
If you are using 'POST' method then normal sending only it works.

Related

Sending XML in Query String

I am integrating USPS Web API in my Java application. I have to send a request of the form
http://production.shippingapis.com/ShippingAPI.dll?API=CityStateLookup&XML=<CityStateLookupRequest%20USERID="XXXNORTH3110"> <ZipCode ID= "0"> <Zip5>22102</Zip5> </ZipCode> </CityStateLookupRequest>
Now hitting this on the browser works fine. But using this from the JAVA code breaks. How can I send XML in the Query String?
Why you should use POST to send this type of data: http://www.w3schools.com/tags/ref_httpmethods.asp
In the POST body you don't need to encode the XML, you just need to set the correct content type "application/xml". Of course that only applies if it is valid XML and does not contain char are not allowed by XML standard.

How to parse a post request from Form to a REST Java/Jersey Webservice

In My Jersey web service, I have this method.
.../rest/services/save
How can I post/parse a long string coming from browser/form without showing it in the url.
String str = "user=mike&param2=value2&param3=value3&............................"
It won't show in the URL if you set the action on the form to POST instead of GET. You will accept #FormParams instead of query params on your server.
You can use a browser extension like POSTMAN:
https://chrome.google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=en
Or use an HTML form.

what is in background of html submit form?

I'm trying to figure out what really happens when html submit form button is clicked.
I suppose it generates some kind of http request (similar to ajax get or post call) which has data in http body and is sent to address specified in action field.
1) Am I right?
2) I've seen many ways of processing forms with PHP or ASP on server side. Can I process it with Java REST Application using e.g. Jersey? Is submit form capable of hitting REST if I put right URL in action field?
Thank You.
By submitting the form in HTML you basically tell the browser to generate a normal HTTP request, usually POST or GET, for an URL defined in tag with form fields attached according to the specified method either appended to the URL or included in the request data.
There is nothing really special or different from a "normal" HTTP request, in fact you can manually "submit a form" by appending form keys and values to the URL in your browser and navigating to it in case of GET method.
Summarizing:
1) Yes, you are right.
2) From what I've just read (never used REST personally) a REST application is implemented by a servlet mechanism and uses HTTP protocol, so it should be possible to write a REST application for processing HTML forms if the form points to this application's URL.

Extracting ViewState when testing JSF with JMeter

I'm using JMeter to do some load tests on my JSF application and I'm having trouble passing the ViewState along the pages. The ViewState variable doesn't get extracted at all or it doesn't get passed along the pages.
I've recorded my test steps with a proxy server and this is what it looks like:
I've added the Regex extractor in the first GET request. Tested the regex and it is correct.
In every POST request I replace the hardwired View IDs with my variable.
And what I get when I send the request is the following:
The POST parameters are incorrect, as it sends the name of the variable.
POST data:
loginForm%3ArequestToken=&loginForm%3Ausername=heller&loginForm%3Apassword=%21QAYxsw2%A7EDC&loginForm%3AloginButton=Anmelden&com.sun.faces.VIEW=%24%7BjsfViewState%7D&loginForm=loginForm
Could you tell what I'm doing wrong here?
Thanks!
The ViewState parameter is an encoded value (Base64 I believe?) and may contain values that would be inappropriate if passed in a GET request through the url. URL parameters are typically encoded so that special values (Eg. space -> %20) can be represented and decoded when the request reaches the server.
The issue here is that the following request is a POST meaning that the parameters do not need to be URL encoded.
com.sun.faces.VIEW=%24%7BjsfViewState%7D&loginForm=loginForm
The above shows that JMeter or some other process is URL encoding the ViewState in the request which is incorrect. The value of the ViewState should simply be sent as is.
Found my problem: the regex was wrong, so it couldn't find anything in the response. I had to change the regex. Noticed it after adding a default value "NOT FOUND".

Send error message as JSON object

I have two servlet: first servlet is similar to a client and creates an HttpURLConnection to call the second servlet.
I would like send a special error, formatted like a JSON object, so I call sendError method in this way:
response.sendError(code, "{json-object}")
But in the first servlet when I read error with getResponseMessage method I just get standard HTTP message and not my json object as a string.
How I can get my json string?
From the HttpServletResponse#sendError() javadoc:
The server defaults to creating the response to look like an HTML-formatted server error page containing the specified message, setting the content type to "text/html", leaving cookies and other headers unmodified. If an error-page declaration has been made for the web application corresponding to the status code passed in, it will be served back in preference to the suggested msg parameter.
So with this approach you have no other option than extracting the message from the HTML response yourself. JSoup may however be useful in this.
To achieve what you want, you need to set the error code and write the response yourself, e.g.
response.setStatus(code);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);
Instead of code you could by the way also use one of the HttpServletResponse.SC_XXX constants for this.

Categories