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.jjtree.utilities.JResponse.java

public static void sendErrorMessage(int errorCode, String errorMessage, HttpServletResponse response)
        throws JSONException, IOException {
    JSONObject resultObject = new JSONObject();

    resultObject.put("errorCode", errorCode);
    resultObject.put("errorMessage", errorMessage);

    PrintWriter writer = response.getWriter();
    writer.print(resultObject);/*ww  w .  j ava2  s  .c o m*/
    writer.flush();
}

From source file:com.intbit.util.ServletUtil.java

public static void printIllegalArgumentError(HttpServletResponse response, String errorMsg) throws IOException {
    response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    Map<String, Object> error = new HashMap<>();
    error.put("error", errorMsg);
    response.getWriter().write(AppConstants.GSON.toJson(error));
    response.getWriter().flush();//from w  w w . ja  v a  2s.  c o m
}

From source file:com.tn.exam.util.web.springside.Struts2Utils.java

/**
 * JSON,Jackson?Java.//from ww w  .j  av  a2s  .  c o  m
 * 
 * @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 {
        mapper.writeValue(response.getWriter(), data);
    } catch (IOException e) {
        throw new IllegalArgumentException(e);
    }
}

From source file:com.intbit.util.ServletUtil.java

public static void printInternalException(HttpServletResponse response, String errorMsg) throws IOException {
    response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
    Map<String, Object> errorMap = new HashMap<>();
    errorMap.put("error", errorMsg);
    response.getWriter().write(AppConstants.GSON.toJson(errorMap));
    response.getWriter().flush();/*from w w w.  j a v a2 s  .c om*/

}

From source file:com.intbit.util.ServletUtil.java

public static void printIllegalArgumentError(HttpServletResponse response, List<String> errorMsgs)
        throws IOException {
    response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
    Map<String, Object> error = new HashMap<>();
    error.put("error", errorMsgs);
    response.getWriter().write(AppConstants.GSON.toJson(error));
    response.getWriter().flush();/*from w ww.j  a  v a  2  s. co m*/
}

From source file:org.eclipse.jgit.lfs.server.fs.FileLfsServlet.java

/**
 * Send an error response./*from  ww w .ja  v a 2 s . com*/
 *
 * @param rsp
 *            the servlet response
 * @param status
 *            HTTP status code
 * @param message
 *            error message
 * @throws IOException
 *             on failure to send the response
 * @since 4.6
 */
protected static void sendError(HttpServletResponse rsp, int status, String message) throws IOException {
    rsp.setStatus(status);
    PrintWriter writer = rsp.getWriter();
    gson.toJson(new Error(message), writer);
    writer.flush();
    writer.close();
    rsp.flushBuffer();
}

From source file:com.eryansky.common.web.struts2.utils.Struts2Utils.java

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

From source file:com.baoqilai.core.util.WebUtils.java

/**
 * .// w ww.  j  a  v  a2 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(final String contentType, final String content, final String... headers) {
    HttpServletResponse response = initResponseHeader(contentType, headers);
    try {
        response.getWriter().write(content);
        response.getWriter().flush();
    } catch (Exception e) {
        logger.warn(e.getMessage(), e);
    }
}

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

private static void sendResponse(SoapMessageImpl responseSoap, HttpServletResponse response) throws Exception {
    log.info("Sending response: {}", responseSoap.getXml());
    try {/*from w w  w  .  j a  v a 2 s  .  c  o m*/
        response.setContentType(MimeTypes.TEXT_XML);
        response.getWriter().write(responseSoap.getXml());
    } finally {
        response.getWriter().close();
    }
}

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

/**
 * ?/*from   w  ww  .j a v a2  s  .  c om*/
 * @param response
 * @param object
 */
public static void responseText(HttpServletResponse response, String text) {
    try {
        response.getWriter().write(text);
        response.getWriter().flush();
        response.getWriter().close();
    } catch (IOException e) {
    }
}