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:edu.cornell.mannlib.vitro.webapp.visualization.visutils.UtilityFunctions.java

public static void handleMalformedParameters(String errorMessage, HttpServletResponse response, Log log)
        throws IOException {

    GenericQueryMap errorDataResponse = new GenericQueryMap();
    errorDataResponse.addEntry("error", errorMessage);

    Gson jsonErrorResponse = new Gson();

    response.setContentType("application/octet-stream");
    response.getWriter().write(jsonErrorResponse.toJson(errorDataResponse));
}

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

protected static void leerConvocatoria(HttpServletRequest request, HttpServletResponse response)
        throws IOException {

    ConvocatoriaEntity e = CtrlUsuario.leerConvocatoria(request.getParameter("1")); // id de la convocatoria

    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    if (e.getNombre().isEmpty()) {
        JSONObject obj = new JSONObject();
        obj.put("isError", true);
        obj.put("errorDescrip", "La convocatoria ya no existe");
        out.print(obj);/* w w  w  .j a  v a2s  . c o  m*/
    } else {
        JSONObject obj = new JSONObject();
        obj.put("id", e.getIdConvocatoria());
        obj.put("nombre", e.getNombre());
        obj.put("descripcion", e.getDescripcion());
        obj.put("cupos", e.getCupos());
        obj.put("fechaFin", "" + e.getFechaFinRegistro());
        out.print(obj);
    }
}

From source file:com.vmware.identity.openidconnect.server.Shared.java

public static void writeJSONResponse(HttpServletResponse httpServletResponse, int statusCode,
        JSONObject jsonObject) throws IOException {
    Validate.notNull(httpServletResponse, "httpServletResponse");
    Validate.notNull(jsonObject, "jsonObject");

    PrintWriter writer = null;//from  w  ww  . ja v a2  s .  com
    try {
        httpServletResponse.setStatus(statusCode);
        httpServletResponse.setContentType("application/json");
        writer = httpServletResponse.getWriter();
        writer.print(jsonObject.toString());
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}

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

protected static void leerUsuariosMultiplesId(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    ArrayList<UsuarioEntity> usuarios = new ArrayList<>();
    usuarios = CtrlAdmin.leerMultiplesUsuarios(Integer.parseInt(request.getParameter("1")),
            Integer.parseInt(request.getParameter("2"))); // id del usuario

    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();

    JSONArray list1 = new JSONArray();
    for (UsuarioEntity usuario : usuarios) {
        JSONObject obj = new JSONObject();
        obj.put("id", usuario.getIdUsuario());
        obj.put("titulo", usuario.getNombres() + " " + usuario.getApellidos());
        list1.add(obj);/*  w w  w  .ja va2 s  .  c  o  m*/
    }
    out.print(list1);
}

From source file:com.google.gsa.valve.modules.utils.HTTPAuthZProcessor.java

/**
 * Includes the HTML document in the response
 * //w ww  .java  2 s .c o m
 * @param response HTTP response
 * @param method HTTP method
 * @param url document url
 * @param loginUrl login url
 * @param contentType content type
 * 
 * @throws IOException
 */
public static void returnHTML(HttpServletResponse response, HttpMethodBase method, String url, String loginUrl,
        String contentType) throws IOException {

    logger.debug("Returning an HTML document");

    //Get writer
    PrintWriter out = null;

    try {
        out = response.getWriter();
        if (out != null) {
            //set content
            out.print(method.getResponseBodyAsString());
            //close writer
            out.close();
            response.setHeader("Content-Type", contentType);
        }
    } catch (Exception e) {
        logger.error("Error when returning HTML content: " + e.getMessage(), e);
        //protection
        if (out != null) {
            out.close();
        }
    }

}

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

protected static void isInscritoTaller(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    boolean b = CtrlUsuario.isInscritoTaller(Integer.parseInt(request.getParameter("1")),
            Integer.parseInt(request.getParameter("2")));
    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    JSONObject obj = new JSONObject();
    obj.put("is", b);
    out.print(obj);/*from www.  j a v  a  2s .co  m*/
}

From source file:myproject.MyServer.java

public static void Delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
    try {//from   ww w.  java2 s  .  c  o m
        String jsonString = IOUtils.toString(request.getInputStream());
        JSONObject json = (JSONObject) JSONValue.parse(jsonString);
        Long student_id = (Long) json.get("student_id");

        String query = String.format("DELETE FROM student " + "WHERE student_id=%d", student_id);

        database.runUpdate(query);

        JSONObject output = new JSONObject();
        output.put("result", true);
        response.getWriter().write(JSONValue.toJSONString(output));
    } catch (Exception ex) {
        JSONObject output = new JSONObject();
        output.put("error", "Connection failed: " + ex.getMessage());
        response.getWriter().write(JSONValue.toJSONString(output));
    }
}

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

protected static void isInscritoconvocatoria(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    boolean b = CtrlUsuario.isInscritoconvocatoria(Integer.parseInt(request.getParameter("1")),
            Integer.parseInt(request.getParameter("2")));
    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    JSONObject obj = new JSONObject();
    obj.put("is", b);
    out.print(obj);/*www.ja v  a 2 s.co m*/
}

From source file:com.sunway.cbm.util.ServletUtils.java

/**
 * ?json/* w ww .  j a v a2 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);
    String jsonStr = JSON.toJSONStringWithDateFormat(object, "yyyy-MM-dd HH:mm:ss", features);
    try {
        response.setContentType("text/plain");
        response.getWriter().write(jsonStr);
        response.getWriter().flush();
        response.getWriter().close();
    } catch (IOException e) {
        logger.error(e.getMessage());
        e.printStackTrace();
    }
    logger.debug(jsonStr);
}

From source file:com.igrow.mall.common.util.Struts2Utils.java

/**
 * .//from  ww w . j a v a2s.  co  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) {
    try {
        //?headers?
        String encoding = ENCODING_DEFAULT;
        boolean noCache = NOCACHE_DEFAULT;
        for (String header : headers) {
            String headerName = StringUtils.substringBefore(header, ":");
            String headerValue = StringUtils.substringAfter(header, ":");

            if (StringUtils.equalsIgnoreCase(headerName, ENCODING_PREFIX)) {
                encoding = headerValue;
            } else if (StringUtils.equalsIgnoreCase(headerName, NOCACHE_PREFIX)) {
                noCache = Boolean.parseBoolean(headerValue);
            } else
                throw new IllegalArgumentException(headerName + "??header");
        }

        HttpServletResponse response = ServletActionContext.getResponse();

        //headers?
        String fullContentType = contentType + ";charset=" + encoding;
        response.setContentType(fullContentType);
        if (noCache) {
            response.setHeader("Pragma", "No-cache");
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expires", 0);
        }

        response.getWriter().write(content);
        response.getWriter().flush();

    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
}