I am trying to send JSON data as a response from a servlet, to Javascript code in my JSP page. I send the data back using the code response.getWriter.write(jsondata);
, where jsondata is the JSON object. What I get back in my Javascript as response is something like [object Object], which is not what I need. Can someone help me understand whats going wrong here?
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
Map<String, Object> data = new HashMap<String, Object>();
data.put("success", true);
data.put("message", "Hello World!");
data.put("param", request.getParameter("user"));
// Write response data as JSON.
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
System.out.println(new Gson().toJson(data));
response.getWriter().write(new Gson().toJson(data));
}
Also, please note that when I use sysout to print the json object it works fine, but when I write it to the response object it gives back [object Object] on the Javascript side.