tags:

views:

30

answers:

2

Hi,

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()

A: 

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
A: 

Jackson is another option for JSON object marshalling.

Steven Benitez