You need to write the content of your JSON object in the response stream. Here's how you can do it:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("application/json");
String output = json.toString();
PrintWriter writer = response.getWriter();
writer.write(output);
writer.close();
}
Also, it's a bad practice to declare non-final objects in your Servlet unless they're managed by the application server like EJBs or DataSources. I'm not sure which tutorial you're following but there are some issues with it:
- We're at Servlet 3.1, and a servlet can be decorated with
@WebServlet rather than use configuration in web.xml.
- You should avoid declaring non-final fields in a Servlet due to concurrency problems. This can be noticed when the field is updated through a GET or POST call to the servlet and multiple (2 or more) clients perform the same call on the servlet at the same time, which will end in odd results for the clients of the servlet.
- If you want/need to obtain data to use in a Servlet, you should obtain it in the narrowest possible scope. In case of Servlets, this means that you should obtain the data in the one of the
doXXX methods.
Here's how your code sample should look like:
@WebServlet("/path")
public class Test extends HttpServlet {
//avoid declaring it here unless it's final
//JSONObject json = new JSONObject();
public void init() throws ServletException {
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//check that now JSONObject was moved as local variable
//for this method and it's initialized with the result
//of a method called retrieveData
//in this way, you gain several things:
//1. Reusability: code won't be duplicated along multiple classes
// clients of this service
//2. Maintainability: centralize how and from where you will
// retrieve the data.
JSONObject json = retrieveData();
response.setContentType("application/json");
String output = json.toString();
PrintWriter writer = response.getWriter();
writer.write(output);
writer.close();
}
//this method will be in charge to return the data you want/need
//for learning purposes, you're hardcoding the data
//in real world applications, you have to retrieve the data
//from a datasource like a file or a database
private JSONObject retrieveData() {
//Initializing the object to return
JSONObject json = new JSONObject();
//filling the object with data
//again, you're hardcoding it for learning/testing purposes
//this method can be modified to obtain data from
//an external resource
json.put("city", "Mumbai");
json.put("country", "India");
//returning the object
return json;
}
public void destroy() {
//do nothing
}
}
outputstring into theresponse