Example usage for javax.servlet.http HttpServletResponse getWriter

List of usage examples for javax.servlet.http HttpServletResponse getWriter

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse getWriter.

Prototype

public PrintWriter getWriter() throws IOException;

Source Link

Document

Returns a PrintWriter object that can send character text to the client.

Usage

From source file:com.age.core.utils.web.ServletUtils.java

/**
 * ?xml?//from   w  w w .  ja va2 s . co  m
 * @param response
 * @param object
 */
public static void responseXml(HttpServletResponse response, String xml) {
    try {
        response.setContentType("text/xml");
        response.getWriter().write(xml);
        response.getWriter().flush();
        response.getWriter().close();
    } catch (IOException e) {
    }
}

From source file:gov.nih.nci.caintegrator.application.util.CSVUtil.java

public static void renderCSV(HttpServletResponse response, List<List> csv) {
    PrintWriter out = null;/*w  w w  .j a  va2 s .com*/

    long randomness = System.currentTimeMillis();
    response.setContentType("application/csv");
    response.setHeader("Content-Disposition", "attachment; filename=report_" + randomness + ".csv");

    try {
        for (List row : csv) {

            out = response.getWriter();
            out.write(StringUtils.join(row.toArray(), ",") + "\r\n");
            out.flush();
        }
    } catch (Exception e) {
        out.write("error generating report");
    }
}

From source file:com.wadpam.guja.oauth2.web.Oauth2ClientAuthenticationFilter.java

private static void errorMessage(HttpServletResponse response, int responseCode, String errorMessage) {
    response.setStatus(responseCode);//from  w  ww .  j a  va  2s . c o  m
    try {
        response.getWriter().write(String.format("{\"error\":\"%s\"}", errorMessage));
        response.setContentType(APPLICATION_JSON_WITH_UTF8_CHARSET);
    } catch (IOException e) {
        LOGGER.error("Failed to write json body {}", e);
    }
}

From source file:cn.guoyukun.spring.web.utils.DownloadUtils.java

public static void download(HttpServletRequest request, HttpServletResponse response, String displayName,
        byte[] bytes) throws IOException {

    if (ArrayUtils.isEmpty(bytes)) {
        response.setContentType("text/html;charset=utf-8");
        response.setCharacterEncoding("utf-8");
        response.getWriter().write("??");
        return;//from w w  w  .j  a  v a  2s . c  om
    }

    String userAgent = request.getHeader("User-Agent");
    boolean isIE = (userAgent != null) && (userAgent.toLowerCase().indexOf("msie") != -1);

    response.reset();
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "must-revalidate, no-transform");
    response.setDateHeader("Expires", 0L);

    response.setContentType("application/x-download");
    response.setContentLength((int) bytes.length);

    String displayFilename = displayName.substring(displayName.lastIndexOf("_") + 1);
    displayFilename = displayFilename.replace(" ", "_");
    if (isIE) {
        displayFilename = URLEncoder.encode(displayFilename, "UTF-8");
        response.setHeader("Content-Disposition", "attachment;filename=\"" + displayFilename + "\"");
    } else {
        displayFilename = new String(displayFilename.getBytes("UTF-8"), "ISO8859-1");
        response.setHeader("Content-Disposition", "attachment;filename=" + displayFilename);
    }
    BufferedInputStream is = null;
    OutputStream os = null;
    try {

        os = response.getOutputStream();
        is = new BufferedInputStream(new ByteArrayInputStream(bytes));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Admin.CRUDUsuarios.java

protected static void obtenerTotalUsuarios(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    int numC = CtrlAdmin.obtenerTotalUsuarios();
    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    JSONObject obj = new JSONObject();
    obj.put("total", numC);
    out.print(obj);/*from  w ww . j ava  2 s . c o  m*/
}

From source file:com.dgq.utils.Struts2Utils.java

/**
 * JSON,Jackson?Java.// ww w. j  a v  a2 s  . c  om
 * 
 * @param data
 *            ?List<POJO>, POJO[], POJO, ?Map??.
 * @see #render(String, String, String...)
 */
public static void renderJson(final Object data, final String... headers) {
    HttpServletResponse response = initResponseHeader(ServletUtils.JSON_TYPE, headers);
    try {
        getObjectMapper().writeValue(response.getWriter(), data);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:com.txtweb.wikipedia.Wikipedia.java

private static void sendResponse(HttpServletResponse httpResponse, String response) {
    try {// ww w . ja  v a  2 s  .  c o m
        httpResponse.setContentType("text/html; charset=UTF-8");
        PrintWriter out = httpResponse.getWriter();

        // Add all the surrounding HTML
        String htmlResponse = "<html><head><title>Wikipedia</title>"
                + "<meta http-equiv='Content-Type' content='text/html; charset=UTF-8' />" + "<meta name='"
                + APPKEY_NAME + "' content='" + APPKEY_CONTENT + "' />" + "</head><body>" + response
                + "</body></html>";

        out.println(htmlResponse);
    } catch (IOException e) {
        //
    }
}

From source file:com.berrysys.ussdgw.HttpUtils.java

/**
 * Queue full logic.//from  ww w.  j a  va2 s  .co m
 *
 * @param resp the resp
 * @throws IOException Signals that an I/O exception has occurred.
 */
private static void queueFullLogic(HttpServletResponse resp) throws IOException {
    // TODO Auto-generated method stub
    String response = "Message Queue full";
    resp.setContentType("text/html");
    resp.setStatus(HttpServletResponse.SC_OK);
    resp.getWriter().println(response);

}

From source file:com.voa.weixin.utils.HttpUtils.java

public static void write(HttpServletResponse response, String message) throws IOException {
    PrintWriter writer = null;/*from  w  w  w . j  a  v  a2  s .  c o m*/
    try {
        response.setHeader("Charset", "UTF-8");
        response.setCharacterEncoding("UTF-8");
        writer = response.getWriter();

        writer.write(message);
    } finally {
        if (writer != null)
            writer.close();
    }
}

From source file:myproject.MyServer.java

public static void Query(HttpServletRequest request, HttpServletResponse response) throws IOException {
    try {/*from w ww .j a va2 s.  co m*/
        String jsonString = IOUtils.toString(request.getInputStream());
        JSONObject json = (JSONObject) JSONValue.parse(jsonString);
        String query = (String) json.get("query");
        String result = database.runQuery(query);
        response.getWriter().write(result);
    } catch (Exception ex) {
        JSONObject output = new JSONObject();
        output.put("error", "Connection failed: " + ex.getMessage());
        response.getWriter().write(JSONValue.toJSONString(output));
    }
}