I am using Extjs 4 and Java Servlets in my app. I want to post some data to the server in json format.
The format of JSON should be like this:
{
"credentials":[
{
"username":"george",
"password":"xyz"
}
]
}
I did this in extjs code:
buttons: [{
text: 'Submit',
handler: function() {
Ext.Ajax.request({
url: '/Model/FormServlet',
method: 'POST',
jsonData : {
//Hardcoded values
username: "george",
password: "xyz"
},
callback: function (options, success, response) {
alert(response.responseText);
}
});
}
}]
1) I believe that I wont get the JSON format which I posted above. Please let me know how to get that format using jsonData.
2) How to retrieve this in servlet doPost method? Since I have not used params and have replaced it with jsonData hence request.getParameter is not going to work in the servlet doPost method.
Please let me know how to make this work.
Regards,
1) You are OK
2) This is a POST request; the data will not come in a parameter (e.g. not multipart/form-data) but will be the content of the HTTP request. In
doPost, simply read everything inrequest.getInputStream()and transform it to String using the proper encoding or callrequest.getContent()directly if you have a properContentHandlerregistered.Something along the lines of
IOUtilsis Apache Commons IO (writing your own is 10 lines with proper Exception handling);TypedContentUtils.extractCharsetis a simple home grown method extracting the charset from the Content-Type or defaulting to utf-8.