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.jaspersoft.jasperserver.rest.RESTUtils.java

/**
 * Set the status of the response to the errorCode and send the message to the client
 *
 * @param errorCode the errorCode (see HttpServletResponse for common HTTP status codes)
 * @param response the HttpServletResponse
 * @param body if null, an empty string is sent.
 *///from   ww w  .  j  av  a  2  s. co m
public static void setStatusAndBody(int errorCode, HttpServletResponse response, String body) {
    response.setStatus(errorCode);
    // we can put an error in the output too (so avoid the webserver to display unwanted pages...)
    try {
        PrintWriter pw = response.getWriter();
        pw.print((body == null) ? "" : body);
    } catch (Exception ioEx) {
        // do nothing. If we have an I/O error, we just avoid to specify unuseful errors here.
        log.error("Error sending output a file", ioEx);
    }
}

From source file:com.easyjf.web.core.FrameworkEngine.java

/**
 * Writer//from   w  w w  .  j av  a2  s  . c  om
 * 
 * @param response
 * @return 
 * @throws UnsupportedEncodingException
 * @throws IOException
 */
public static Writer getResponseWriter(HttpServletResponse response)
        throws UnsupportedEncodingException, IOException {
    Writer writer = null;
    try {
        writer = response.getWriter();
    } catch (IllegalStateException e) {
        String encoding = response.getCharacterEncoding();
        if (encoding == null) {
            encoding = DEFAULT_OUTPUT_ENCODING;
        }
        writer = new OutputStreamWriter(response.getOutputStream(), encoding);
    }
    return writer;
}

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

protected static void registrarDocenteTaller(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    ArrayList r = CtrlUsuario.registrarATallerDocente(Integer.parseInt(request.getParameter("1")),
            Integer.parseInt(request.getParameter("2"))); // parameter 1: idUsuarioDocente param2: idTaller

    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);/*from  w w  w . j av  a 2s . co m*/
    } 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:com.sunway.cbm.util.ServletUtils.java

/**
 * ?xml?//from  w w  w.  j  a  v  a 2  s.c  om
 * 
 * @param response
 * @param object
 */
public static void responseXml(HttpServletResponse response, String xml) {
    try {
        //         response.setContentType("text/xml");
        response.setCharacterEncoding("UTF-8");
        logger.debug(xml);
        //         response.getWriter().write(xml);
        //         response.getWriter().flush();
        //         response.getWriter().close();
        PrintWriter out = response.getWriter();
        out.print(xml);
        out.close();
    } catch (IOException e) {
        logger.debug(e.toString());
    }
}

From source file:net.sourceforge.fenixedu.presentationTier.servlets.filters.JerseyOAuth2Filter.java

private static boolean sendError(final HttpServletResponse response, String error, String errorDescription)
        throws IOException, ServletException {
    OAuthResponse errorResponse;//from w ww.ja  v a  2s.  co m
    try {
        errorResponse = new OAuthErrorResponseBuilder(401).setError(error).setErrorDescription(errorDescription)
                .buildJSONMessage();
        response.setContentType("application/json; charset=UTF-8");
        response.setStatus(errorResponse.getResponseStatus());
        PrintWriter pw = response.getWriter();
        pw.print(errorResponse.getBody());
        return false;
    } catch (OAuthSystemException e) {
        throw new ServletException(e);
    }
}

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

protected static void iniciarWSC(HttpServletRequest request, HttpServletResponse response) throws IOException {
    ArrayList<UsuarioEntity> usuarios = CtrlAdmin.iniciarWSC(Integer.parseInt(request.getParameter("1")), //id evento
            Integer.parseInt(request.getParameter("2")), //tamao tabla
            Integer.parseInt(request.getParameter("3"))//pagina
    ); // parameter 1: documentoDocente param2: idTaller

    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);/*from   ww w. j a v  a 2 s. c om*/
    }
    out.print(list1);
}

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

protected static void leerMultiplesTalleres(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    ArrayList<TallerEntity> talleres = new ArrayList<>();
    talleres = CtrlUsuario.leerMultiplesTalleres(Integer.parseInt(request.getParameter("1")),
            Integer.parseInt(request.getParameter("2"))); // posicin y tamao

    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    System.out.println("Talleres: " + talleres.size());
    JSONArray list1 = new JSONArray();
    for (TallerEntity taller : talleres) {
        JSONObject obj = new JSONObject();
        obj.put("id", taller.getIdTaller());
        obj.put("titulo", taller.getNombre());
        list1.add(obj);/*  w w w.ja va 2s . com*/
    }
    out.print(list1);
}

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

protected static void consultarUsuariosEnConvocatoriaId(HttpServletRequest request,
        HttpServletResponse response) throws IOException {
    ArrayList<UsuarioEntity> usuarios = new ArrayList<>();
    usuarios = CtrlAdmin.obtenerUsuariosEnConvocatoria(Integer.parseInt(request.getParameter("3")), //id evento
            Integer.parseInt(request.getParameter("2")), //tamao tabla
            Integer.parseInt(request.getParameter("1"))//pagina
    );/*from w  w w  .  j  av  a 2s .c om*/

    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);
    }
    out.print(list1);
}

From source file:com.runwaysdk.controller.ErrorUtility.java

/**
 * Handles errors that are generated from a request sent asynchronously from the RunwayControllerForm js widget.
 * //from w  w w  . ja  v a2  s  .  c o  m
 * @param t
 * @param req
 * @param resp
 * @throws IOException
 * @returns boolean Whether or not a redirect is required. A redirect is required if and only if the error is inlined.
 */
public static boolean handleFormError(Throwable t, HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    t = ErrorUtility.filterServletException(t);

    if (t instanceof ProblemExceptionDTO) {
        ErrorUtility.prepareProblems((ProblemExceptionDTO) t, req, false);
        return true;
    } else {
        JSONRunwayExceptionDTO jsonE = new JSONRunwayExceptionDTO(t);
        resp.setStatus(500);
        resp.getWriter().print(jsonE.getJSON());
        return false;
    }
}

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

protected static void leerMultiplesConvocatorias(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    ArrayList<ConvocatoriaEntity> convocatorias = new ArrayList<>();
    convocatorias = CtrlUsuario.leerMultiplesConvocatorias(Integer.parseInt(request.getParameter("1")),
            Integer.parseInt(request.getParameter("2"))); // tamao y posicin

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

    JSONArray list1 = new JSONArray();
    for (ConvocatoriaEntity convocatoria : convocatorias) {
        JSONObject obj = new JSONObject();
        obj.put("id", convocatoria.getIdConvocatoria());
        obj.put("titulo", convocatoria.getNombre());
        list1.add(obj);//from   ww w . j  a  v  a 2s  . co  m
    }
    out.print(list1);
}