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:org.esgf.legacydatacart.WgetGeneratorController.java

private static void writeBash(String wgetText, String filename, HttpServletResponse response) {
    try {//from   w  w w .ja  v  a  2  s .  co m
        //attach the sh file extension to the response
        response.setContentType("text/x-sh");
        response.addHeader("Content-Disposition", "attachment; filename=" + filename);
        response.setContentLength((int) wgetText.length());

        PrintWriter out = response.getWriter();
        out.print(wgetText);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

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

/**
 * .//ww w  . j a va 2  s. co  m
 *
 * @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.palantir.gerrit.gerritci.servlets.JobsServlet.java

public static boolean safetyCheck(int responseCode, HttpServletResponse res, String projectName)
        throws IOException {
    if (responseCode != 200) {
        JsonObject errorMsg = new JsonObject();
        if (responseCode == 404) {
            logger.error("Could not find project with name: " + projectName);
            errorMsg = makeErrorJobObject("Could not find project with name: " + projectName);
            res.getWriter().write(errorMsg.toString());
        } else {/*from  w w  w.  j  a v a 2s  .co m*/
            logger.error("User Authentication Error ");
            errorMsg = makeErrorJobObject("Permission Denied: User Authentication Error ");
            res.getWriter().write(errorMsg.toString());
        }
        return false;
    }
    return true;
}

From source file:myproject.MyServer.java

public static void Update(HttpServletRequest request, HttpServletResponse response) throws IOException {
    try {//w ww .ja  va 2 s  .co  m
        String jsonString = IOUtils.toString(request.getInputStream());
        JSONObject json = (JSONObject) JSONValue.parse(jsonString);
        Long student_id = (Long) json.get("student_id");
        String student_name = (String) json.get("student_name");
        Long regno = (Long) json.get("regno");
        Double cgpa = (Double) json.get("cgpa");

        String query = String.format(
                "UPDATE student " + "SET student_name='%s'," + "regno=%d," + "cgpa=%f " + "WHERE student_id=%d",
                JSONValue.escape(student_name), regno, cgpa, student_id);

        database.runUpdate(query);

        String result = database.getStudent(regno);
        response.getWriter().write(result);
    } 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.Admin.CRUDUsuarios.java

protected static void crearUsuario(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    System.out.println(request.getParameter("5"));
    ArrayList r = CtrlAdmin.crearUsuario(request.getParameter("1"), //Nombre
            request.getParameter("2"), //Apellidos
            request.getParameter("3"), //Tipo de documento
            request.getParameter("4"), //documento
            request.getParameter("5"), //Correo= usuario
            request.getParameter("6"), //passworld
            request.getParameter("7").charAt(0)//rol
    );/*from ww  w .  j av  a2s.c  o  m*/

    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    if (r.get(0) == "error") {
        JSONObject obj = new JSONObject();
        if (r.get(1) == "usuario") {
            obj.put("isError", true);
            obj.put("errorDescrip", "El usuario ya existe");
        } else if (r.get(1) == "contrasena") {
            obj.put("isError", true);
            obj.put("errorDescrip", "La contrasea es invalida");
        } else if (r.get(1) == "documento") {
            obj.put("isError", true);
            obj.put("errorDescrip", "El documento ya est registrado");
        } else if (r.get(1) == "tipoDocumento") {
            obj.put("isError", true);
            obj.put("errorDescrip", "El tipo de documento es invalido");
        } else if (r.get(1) == "correo") {
            obj.put("isError", true);
            obj.put("errorDescrip", "El correo ya est registrado");
        } else if (r.get(1) == "correo1") {
            obj.put("isError", true);
            obj.put("errorDescrip", "El correo no es valido");
        } else if (r.get(1) == "nombre") {
            obj.put("isError", true);
            obj.put("errorDescrip", "Los nombres o apellidos son incorrectos");
        } else if (r.get(1) == "rol") {
            obj.put("isError", true);
            obj.put("errorDescrip", "El rol es invalido, los posibles valores son: E, P y A");
        } else
            Util.errordeRespuesta(r, out);
        out.print(obj);
    } 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:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Admin.CUDEventos.java

protected static void crearTaller(HttpServletRequest request, HttpServletResponse response) throws IOException {
    ArrayList r = CtrlAdmin.crearTaller(request.getParameter("1"), // nombre
            request.getParameter("2"), // descripcin
            request.getParameter("4"), // fin registro (Fecha hasta donde est permitido registrarse)
            request.getParameter("3"), // inicio del taller
            request.getParameter("4"), // fin del taller
            Integer.parseInt(request.getParameter("5")), // costo
            Integer.parseInt(request.getParameter("6")) // cupos
    );//from   w w  w.j  a v  a2  s .  co  m

    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);
    } 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.sunchenbin.store.feilong.servlet.http.ResponseUtil.java

/**
 * .//  w  w  w.  j  av  a 2 s. c  om
 *
 * @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:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Admin.CUDEventos.java

protected static void actualizarTaller(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    ArrayList r = CtrlAdmin.actualizarTaller(Integer.parseInt(request.getParameter("0")), // id
            request.getParameter("1"), // nombre
            request.getParameter("2"), // descripcin
            request.getParameter("3"), // fin registro (Fecha hasta donde est permitido registrarse)
            request.getParameter("4"), // inicio del taller
            Integer.parseInt(request.getParameter("5")), // costo
            Integer.parseInt(request.getParameter("6")) // cupos
    );/*from  w w  w.  j  a  va2 s  . co m*/

    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);
    } 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:de.escidoc.core.aa.servlet.Login.java

/**
 * Writes the provided page content to the writer of the {@link HttpServletResponse}. The http status code is set to
 * the provided value.//from  w  w w  .  j a v  a  2s.  c  om
 *
 * @param response   The {@link HttpServletResponse}.
 * @param page       The page to write.
 * @param statusCode The http status code to set.
 * @throws IOException Thrown in case of a failed i/o operation.
 */
private static void sendResponse(final HttpServletResponse response, final String page, final int statusCode)
        throws IOException {
    final PrintWriter writer = response.getWriter();
    writer.print(page);
    response.setStatus(statusCode);
}

From source file:de.escidoc.core.aa.servlet.Login.java

/**
 * Initializes the redirect to the provided URL and writes the provided page content to the writer of the {@link
 * HttpServletResponse}. The http status code is set to SEE_OTHER.
 *
 * @param response    The {@link HttpServletResponse}.
 * @param page        The page to write.
 * @param redirectUrl The URL to that the user shall be redirected.
 * @throws IOException Thrown in case of a failed i/o operation.
 *//*from w w w  .j av a  2 s  .  co  m*/
private static void sendRedirectingResponse(final HttpServletResponse response, final String page,
        final String redirectUrl) throws IOException {

    final PrintWriter writer = response.getWriter();
    writer.print(page);
    // TODO: which response/status code? sendRedirect uses 302. 307 must
    // not be used since this results in forwarding the post of login data.
    // Maybe, 303 should be used to force the browser to redirect with GET,
    // as redirect with POST is prohibited?
    // Or should a form be returned and the user posts the handle back to
    // the application (similar to Shibboleth's Browser/Post profile)?
    response.setStatus(HttpServletResponse.SC_SEE_OTHER);
    response.setHeader("Location", redirectUrl);
    //        response.flushBuffer();
}