In front of us, we talked about two kinds of books, the first is the Key-value form, the second is the JSON format,
For the first we use Request.getparameter ("key") directly on the server side, we can get the value corresponding to the key. However, you cannot use this code for the JSON that is passed.
The parameters passed by JSON exist in the body of the request, we need to read from the inside input stream, here is the code:
1 /**2 * Get the requested body3 * @paramreq4 * @return5 * @throwsIOException6 */7 Public StaticString Getrequestbody (httpservletrequest req)throwsIOException {8BufferedReader reader =Req.getreader ();9String input =NULL;TenStringBuffer requestbody =NewStringBuffer (); One while(input = Reader.readline ())! =NULL) { A requestbody.append (input); - } - returnrequestbody.tostring (); the}
StringBuffer requestbody = new StringBuffer (); Note that some use string to define Requestbody, which can have a disadvantage,
Each time Reader.readline () is not empty, requestbody + = input will create a new string object, and StringBuffer can be appended.
Java Web server-side processing JSON format parameters