Example usage for javax.servlet.http HttpServletResponse setCharacterEncoding

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

Introduction

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

Prototype

public void setCharacterEncoding(String charset);

Source Link

Document

Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8.

Usage

From source file:com.github.rnewson.couchdb.lucene.util.ServletUtils.java

public static void setResponseContentTypeAndEncoding(final HttpServletRequest req,
        final HttpServletResponse resp) {
    final String accept = req.getHeader("Accept");
    if (getBooleanParameter(req, "force_json") || (accept != null && accept.contains("application/json"))) {
        resp.setContentType("application/json");
    } else {// w w  w .j a  v  a  2s . c  o m
        resp.setContentType("text/plain");
    }
    if (!resp.containsHeader("Vary")) {
        resp.addHeader("Vary", "Accept");
    }
    resp.setCharacterEncoding("utf-8");
}

From source file:org.bonitasoft.web.designer.controller.utils.HttpFile.java

/**
 * Write headers and content in the response
 *///from   w w  w .  j av  a  2  s.c om
private static void writeFileInResponse(HttpServletResponse response, Path filePath, String mimeType,
        String contentDispositionType) throws IOException {
    response.setHeader("Content-Type", mimeType);
    response.setHeader("Content-Length", String.valueOf(filePath.toFile().length()));
    response.setHeader("Content-Disposition", new StringBuilder().append(contentDispositionType)
            .append("; filename=\"").append(filePath.getFileName()).append("\"").toString());
    response.setCharacterEncoding(StandardCharsets.UTF_8.toString());
    try (OutputStream out = response.getOutputStream()) {
        Files.copy(filePath, out);
    }
}

From source file:ee.ria.xroad.common.request.DummyCentralServiceHandler.java

private static void sendErrorResponse(HttpServletResponse response, String faultCode, String faultString,
        String faultActor, String faultDetail) throws IOException {
    String soapMessageXml = SoapFault.createFaultXml(faultCode, faultString, faultActor, faultDetail);

    String encoding = StandardCharsets.UTF_8.name();
    byte[] messageBytes = soapMessageXml.getBytes(encoding);

    response.setStatus(HttpServletResponse.SC_OK);
    response.setContentType(MimeTypes.TEXT_XML);
    response.setContentLength(messageBytes.length);
    response.setHeader("SOAPAction", "");
    response.setCharacterEncoding(encoding);
    response.getOutputStream().write(messageBytes);
}

From source file:com.discovery.darchrow.http.ResponseUtil.java

/**
 * .//ww w.  j  a va2  s.  c  om
 *
 * @param response
 *            HttpServletResponse
 * @param content
 *            
 * @param contentType
 *            the content type
 * @param characterEncoding
 *            the character encoding
 * @throws UncheckedIOException
 *             the unchecked io exception
 * @see javax.servlet.ServletResponse#getWriter()
 * @see java.io.PrintWriter#print(Object)
 * @see java.io.PrintWriter#flush()
 * @since 1.0.9
 */
public static void write(HttpServletResponse response, Object content, String contentType,
        String characterEncoding) throws UncheckedIOException {
    try {
        //? ? getWriter?
        if (Validator.isNotNullOrEmpty(contentType)) {
            response.setContentType(contentType);
        }
        if (Validator.isNotNullOrEmpty(characterEncoding)) {
            response.setCharacterEncoding(characterEncoding);
        }

        PrintWriter printWriter = response.getWriter();
        printWriter.print(content);
        printWriter.flush();

        //http://www.iteye.com/problems/56543
        //tomcatjetty?? printWriter.close();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:com.sunchenbin.store.feilong.servlet.http.ResponseUtil.java

/**
 * ./*  w w  w  . j  a  v a2 s.co  m*/
 *
 * @param response
 *            HttpServletResponse
 * @param content
 *            
 * @param contentType
 *            the content type
 * @param characterEncoding
 *            the character encoding
 * @see javax.servlet.ServletResponse#getWriter()
 * @see java.io.PrintWriter#print(Object)
 * @see java.io.PrintWriter#flush()
 * @since 1.0.9
 */
public static void write(HttpServletResponse response, Object content, String contentType,
        String characterEncoding) {
    try {
        //? ? getWriter?
        if (Validator.isNotNullOrEmpty(contentType)) {
            response.setContentType(contentType);
        }
        if (Validator.isNotNullOrEmpty(characterEncoding)) {
            response.setCharacterEncoding(characterEncoding);
        }

        PrintWriter printWriter = response.getWriter();
        printWriter.print(content);
        printWriter.flush();

        //http://www.iteye.com/problems/56543
        //tomcatjetty?? printWriter.close();
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }
}

From source file:ee.ria.xroad.common.conf.globalconf.ConfigurationClientMain.java

private static void setup() throws Exception {
    log.trace("setUp()");

    client = createClient();/*from w  w w.j ava 2 s .c o m*/

    adminPort = new AdminPort(SystemProperties.getConfigurationClientAdminPort());

    adminPort.addShutdownHook(() -> {
        log.info("Configuration client shutting down...");
        try {
            shutdown();
        } catch (Exception e) {
            log.error("Error while shutting down", e);
        }
    });

    adminPort.addHandler("/execute", new AdminPort.SynchronousCallback() {
        @Override
        public void handle(HttpServletRequest request, HttpServletResponse response) {
            log.info("handler /execute");
            try {
                client.execute();
            } catch (Exception e) {
                throw translateException(e);
            }
        }
    });

    adminPort.addHandler("/status", new AdminPort.SynchronousCallback() {
        @Override
        public void handle(HttpServletRequest request, HttpServletResponse response) {
            try {
                log.info("handler /status");
                response.setCharacterEncoding("UTF8");
                JsonUtils.getSerializer().toJson(ConfigurationClientJobListener.getStatus(),
                        response.getWriter());
            } catch (Exception e) {
                log.error("Error getting conf client status", e);
            }
        }
    });
}

From source file:com.dien.upload.server.UploadServlet.java

/**
 * Writes a response to the client./*  w  ww. j  ava 2  s  .  c o m*/
 */
protected static void renderMessage(HttpServletResponse response, String message, String contentType)
        throws IOException {
    response.setContentType(contentType + "; charset=UTF-8");
    response.setCharacterEncoding("UTF-8");
    PrintWriter out = response.getWriter();
    out.print(message);
    out.flush();
    out.close();
}

From source file:elw.web.ControllerElw.java

public static void storeContentHeaders(final FileBase fileBase, final HttpServletResponse resp) {
    final FileType fileType = IdNamed._.one(fileBase.getFileType());
    final String contentType;
    if (!fileType.getContentTypes().isEmpty()) {
        contentType = fileType.getContentTypes().get(0);
    } else {//  w ww. j ava  2  s .com
        contentType = fileType.isBinary() ? "binary/octet-stream" : "text/plain";
    }

    if (fileType.isBinary()) {
        resp.setContentType(contentType);
    } else {
        resp.setContentType(contentType + "; charset=UTF-8");
        resp.setCharacterEncoding("UTF-8");
    }

    resp.setContentLength(fileBase.getCouchFile(FileBase.CONTENT).getLength().intValue());
    resp.setHeader("Content-Disposition", "attachment;");
}

From source file:com.kolich.spring.views.mappers.KolichMappingHTMLView.java

@Override
public void myPrepareResponse(final KolichViewSerializable payload, final HttpServletRequest request,
        final HttpServletResponse response) {
    response.setCharacterEncoding(UTF_8);
}

From source file:cz.muni.fi.pb138.callboxesmap.CallboxesServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setCharacterEncoding(CallboxesXMLClass.CHARACTER_ENCODING);
    PrintWriter writer = resp.getWriter();
    writer.append(getJsonCallboxes().toString());
}