2

Firstly, my HTTP POST through a URL accepts 4 parameters. (Param1, Param2, Param3, Param4).

Can I pass the parameters from the database?

Once the URL is entered, the information returned will be in text format using JSON format.

The JSON will return either {"Status" : "Yes"} or {"Status" : "No"}

How shall I do this in servlets? doPost()

user448402
  • 159
  • 1
  • 18

2 Answers2

2

Just set the proper content type and encoding and write the JSON string to the response accordingly.

String json = "{\"status\": \"Yes\"}";
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
response.getWriter().write(json);

Instead of composing the JSON yourself, you may consider using an existing JSON library to ease the job of JSON (de)serializing in Java. For example Google Gson.

Map<String, String> result = new HashMap<String, String>();
result.put("status", "Yes");
// ... (put more if necessary)

String json = new Gson().toJson(result);
// ... (just write to response as above)
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

Jackson is another option for JSON object marshalling.

Steven Benitez
  • 10,936
  • 3
  • 39
  • 50