output Json from HttpServletResponse - Java Servlet JSP

Java examples for Servlet JSP:Servlet

Description

output Json from HttpServletResponse

Demo Code


import org.apache.log4j.Logger;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Set;

public class Main{
    private static final Logger logs = Logger.getLogger(ResponseUtil.class);
    public static void outJson(HttpServletResponse response, String json) {
        response.setHeader("Cache-Control", "no-cache");
        response.setContentType("application/json;charset=UTF-8");
        try {// www .j  a  va  2  s . com
            logs.debug(json);
            response.getWriter().write(json);
            response.getWriter().flush();
            response.getWriter().close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

Related Tutorials