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.lll.util.ServletUtils.java

/**
 * .//from   ww w  . ja va2  s  .c o m
        
 * eg.
 * render("text/plain", "hello", "encoding:GBK");
 * render("text/plain", "hello", "no-cache:false");
 * render("text/plain", "hello", "encoding:GBK", "no-cache:false");
 * 
 * @param headers ??header??"encoding:""no-cache:",UTF-8true.
 */
public static void render(HttpServletResponse response, final String contentType, final String content,
        final String... headers) {
    initResponseHeader(response, contentType, headers);
    try {
        response.getWriter().write(content);
        response.getWriter().flush();
    } catch (IOException e) {
        throw new RuntimeException(e.getMessage(), e);
    }
}

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

protected static void eliminarConvocatoria(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    ArrayList r = CtrlAdmin.eliminarConvocatoria(Integer.parseInt(request.getParameter("1"))); // id

    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    if (r.get(0) == "error") {
        JSONObject obj = new JSONObject();
        obj.put("isError", true);
        obj.put("errorDescrip", r.get(1));
        out.print(obj);/*  ww  w.  j a  va  2s.c om*/
    } else if (r.get(0) == "isExitoso") {
        JSONObject obj = new JSONObject();
        obj.put("Exitoso", true);
        out.print(obj);
    } else
        Util.errordeRespuesta(r, out);
}

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

public static void download(HttpServletRequest request, HttpServletResponse response, String filePath,
        String displayName) throws IOException {
    File file = new File(filePath);

    if (StringUtils.isEmpty(displayName)) {
        displayName = file.getName();//from w w  w .j a  v a2s.c  o m
    }

    if (!file.exists() || !file.canRead()) {
        response.setContentType("text/html;charset=utf-8");
        response.getWriter().write("??");
        return;
    }

    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) file.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 FileInputStream(file));
        IOUtils.copy(is, os);
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(is);
    }
}

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

/**
 * ?json/*w ww. j a va  2 s  . c  o m*/
 * @param response
 * @param object
 */
public static void responseJson(HttpServletResponse response, Object object) {
    JsonMapper jsonMapper = JsonMapper.buildNormalMapper();
    String jsonStr = jsonMapper.toJson(object);
    try {
        response.setContentType("text/plain");
        response.getWriter().write(jsonStr);
        response.getWriter().flush();
        response.getWriter().close();
    } catch (IOException e) {
    }
}

From source file:com.aaasec.sigserv.csspserver.SpServlet.java

private static void respond(HttpServletResponse response, String responseData) {
    try {// w  w w  . ja v a2s. c  om
        response.getWriter().write(responseData);
        response.getWriter().close();
    } catch (IOException ex) {
        Logger.getLogger(SpServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Usuario.Read.java

protected static void obtenerTotalTalleres(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    int numC = CtrlUsuario.obtenerTotalTalleres();
    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  a  va2 s .c  o m
}

From source file:com.liusoft.dlog4j.action.ActionBase.java

/**
 * ??/* w w w.  ja va 2 s.  c  o m*/
 * @param res
 * @param msg
 * @throws IOException
 */
protected static void outputPlainMsg(HttpServletResponse res, String msg) throws IOException {
    res.setContentType("text/plain;charset=UTF-8");
    res.getWriter().print(msg);
}

From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Usuario.Read.java

protected static void obtenerTotalConvocatorias(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    int numC = CtrlUsuario.obtenerTotalConvocatorias();
    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    JSONObject obj = new JSONObject();
    obj.put("total", numC);
    out.print(obj);//from   www .j  a  v a2 s .  com
}

From source file:com.eucalyptus.www.X509Download.java

public static void hasError(int statusCode, String message, HttpServletResponse response) {
    try {/*from  w ww  .  j  a  v a  2s  .  c o m*/
        response.setStatus(statusCode);
        response.setContentType("text/plain; charset=utf-8");
        response.getWriter().print(getError(message));
        response.getWriter().flush();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:gov.nist.appvet.tool.synchtest.util.ReportUtil.java

/**
 * This method returns report information to the AppVet ToolAdapter as ASCII
 * text and cannot attach a file to the response.
 *//*from ww w.  j  ava  2 s  .c om*/
public static boolean sendInHttpResponse(HttpServletResponse response, String reportText,
        ToolStatus reportStatus) {
    try {
        response.setStatus(HttpServletResponse.SC_OK); // HTTP 200
        response.setContentType("text/html");
        response.setHeader("apprisk", reportStatus.name());
        PrintWriter out = response.getWriter();
        out.println(reportText);
        out.flush();
        out.close();
        log.info("Returned report");
        return true;
    } catch (IOException e) {
        log.error("Report not sent: " + e.toString());
        return false;
    }
}