response.setContentType("text/html; charset=UTF-8"); PrintWriter out = response.getWriter(); out.println("Hello World!
");
response.setContentType("application/json"); PrintWriter out = response.getWriter(); JsonObject obj = new JsonObject(); obj.addProperty("name", "John"); obj.addProperty("age", 30); out.println(obj.toString());This code snippet sets the content type to JSON and sends a JSON response to the client using the Gson library, which is part of the com.google.gson package. In both examples, the getWriter() method is used to obtain a PrintWriter object, which is then used to send the response to the client using the println() method. The package library used in these examples is javax.servlet.http.