jaume jaume - 4 years ago 143
JSON Question

Send a simple JSON object in Java to a Servlet

I'm trying to send a JSONObject (org.json) from a java client to a servlet, but in my server side i get 'null' for HttpServletRequest.getParameter("Command") or any parameter.

In my client side:

JSONObject json = new JSONObject();
try{
json.put("Command","spost");
json.put("Name","pc1");
json.put("Pwd","pc1");
sendRequest(json);
} catch(JSONException jsone){

}
URL url;
HttpURLConnection connection = null;
ObjectOutputStream out;
try {
url = new URL("http://myURL.com/myservlet"); //Creating the URL.
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
//connection.setRequestProperty("Accept", "application/json");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
//Send request
OutputStream os = connection.getOutputStream();
OutputStreamWriter osw = new OutputStreamWriter(os, "UTF-8");
System.out.println(json.toString());
osw.write(json.toString());
osw.flush();
osw.close();
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
System.out.println("Ok response");
} else {
System.out.println("Bad response");
}
} catch (Exception ex) {
ex.printStackTrace();
}


Then I get something like this when printing json.toString():

{"Name":"pc1","Command":"SignalPost","Pwd":"pc1"}


...that looks pretty normal to me.

I see an "Ok response" and my servlet detects the httprequest, but seems there is something wrong understanding the json object

My servlet is working fine for another client i made using AJAX, so i guess the problem is in this java client.

Could you please help me? I googled and tried everything with no luck

Thanks

EDIT:

At the end, in server-side, this code is working:

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try{
StringBuilder sb = new StringBuilder();
BufferedReader br = request.getReader();
String str = null;
while ((str = br.readLine()) != null) {
sb.append(str);
System.out.println(str);
}
JSONObject jObj = new JSONObject(sb.toString());
String name = jObj.getString("Name");
String pwd = jObj.getString("Pwd");
String command = jObj.getString("Command");

JSONObject json = new JSONObject();
response.setContentType("application/json");
response.setHeader("Cache-Control", "nocache");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
out.print(json.toString());
} catch (Exception e) {
e.printStackTrace();
}
}


Although i would prefer to keep using GET request from clients so that i don't have to remake all my servlet side

Answer Source

Your json object is not being sent as a request param, it is sent in the request body.

So, in your server side servlet you do not have to try to recover it from any request param, you must read it from the HttpServletRequest's InputStream.

Read it and then parse it using the json library you have chosen in your servlet method and you will get it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download