Skip to content

Instantly share code, notes, and snippets.

@rafayali
Last active August 29, 2015 14:10
Show Gist options
  • Save rafayali/0da3ea48acb2f5a7e6dc to your computer and use it in GitHub Desktop.
Save rafayali/0da3ea48acb2f5a7e6dc to your computer and use it in GitHub Desktop.
/**
* Sends data back to client by using HTTPServletResponse object and adding data into JSON Array
* then flushes out the stream and closes the response
* Sends data
* @param resp
* @param result
* @param message
* @param stringData
* @throws IOException
*/
public static void sendData(HttpServletResponse resp, String result, String message, List<String> stringData) throws IOException {
JSONArray data = new JSONArray();
data.put(result);
data.put(message);
if(stringData != null)
for(String s : stringData){
data.put(s);
}
final String output = data.toString();
resp.setContentLength(output.length());
resp.getOutputStream().write(output.getBytes());
resp.getOutputStream().flush();
resp.getOutputStream().close();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment