Example usage for javax.servlet ServletOutputStream write

List of usage examples for javax.servlet ServletOutputStream write

Introduction

In this page you can find the example usage for javax.servlet ServletOutputStream write.

Prototype

public abstract void write(int b) throws IOException;

Source Link

Document

Writes the specified byte to this output stream.

Usage

From source file:pt.ist.fenix.ui.struts.action.externalServices.ExternalInterfaceDispatchAction.java

protected void writeJSONObject(HttpServletResponse response, final JSONObject jsonObject) throws IOException {
    response.setContentType("application/json; charset=utf-8");
    final ServletOutputStream outputStream = response.getOutputStream();
    outputStream.write(jsonObject.toJSONString().getBytes());
    outputStream.close();/*from ww  w .  ja v  a  2s .  c o  m*/
}

From source file:net.sf.appstatus.demo.batch.LaunchClassicBatchSampleServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    // launch the batch
    ExecutorService executorService = Executors.newCachedThreadPool();

    BatchSample batch = (BatchSample) WebApplicationContextUtils
            .getRequiredWebApplicationContext(getServletContext()).getBean("batch");
    executorService.execute(batch);/*  www.  ja  va2 s  .c  o  m*/

    BatchSample2 batch2 = (BatchSample2) WebApplicationContextUtils
            .getRequiredWebApplicationContext(getServletContext()).getBean("batch2");
    executorService.execute(batch2);

    ServletOutputStream os = resp.getOutputStream();

    os.write("<html><head".getBytes(ENCODING));
    os.write("<body>".getBytes(ENCODING));
    os.write("<h1>Ok</h1>".getBytes(ENCODING));
    os.write("</body></html>".getBytes(ENCODING));
}

From source file:be.fedict.eid.dss.sp.servlet.DownloadServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    LOG.debug("doGet");
    HttpSession httpSession = request.getSession();
    byte[] document = (byte[]) httpSession.getAttribute("document");

    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=-1"); // http 1.1
    if (!request.getScheme().equals("https")) {
        // else the download fails in IE
        response.setHeader("Pragma", "no-cache"); // http 1.0
    } else {//w ww  .  j a v  a 2s.c o m
        response.setHeader("Pragma", "public");
    }
    response.setDateHeader("Expires", -1);
    response.setContentLength(document.length);

    String contentType = (String) httpSession.getAttribute("ContentType");
    LOG.debug("content-type: " + contentType);
    response.setContentType(contentType);
    response.setContentLength(document.length);
    ServletOutputStream out = response.getOutputStream();
    out.write(document);
    out.flush();
}

From source file:net.jadler.stubbing.server.jetty.StubHandler.java

private void writeResponseBody(final byte[] body, final HttpServletResponse response) throws IOException {
    if (body.length > 0) {
        final ServletOutputStream os = response.getOutputStream();
        os.write(body);
    }/*w w  w. j a v  a  2  s  . com*/
}

From source file:be.fedict.eid.dss.portal.DownloadServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    LOG.debug("doGet");
    HttpSession httpSession = request.getSession();
    byte[] document = (byte[]) httpSession.getAttribute(this.documentSessionAttribute);

    response.setHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=-1"); // http 1.1
    if (false == request.getScheme().equals("https")) {
        // else the download fails in IE
        response.setHeader("Pragma", "no-cache"); // http 1.0
    } else {// w  w  w  . j  a v  a2  s  .  c  om
        response.setHeader("Pragma", "public");
    }
    response.setDateHeader("Expires", -1);
    response.setContentLength(document.length);

    String contentType = (String) httpSession.getAttribute(this.contentTypeSessionAttribute);
    LOG.debug("content-type: " + contentType);
    response.setContentType(contentType);
    response.setContentLength(document.length);
    ServletOutputStream out = response.getOutputStream();
    out.write(document);
    out.flush();
}

From source file:be.fedict.eid.applet.service.PdfServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    LOG.debug("doGet");

    HttpSession httpSession = request.getSession();
    EIdData eIdData = (EIdData) httpSession.getAttribute("eid");

    byte[] document;
    try {//w w  w  .  j a  v  a2  s.co m
        document = this.pdfGenerator.generatePdf(eIdData);
    } catch (DocumentException e) {
        throw new ServletException("PDF generator error: " + e.getMessage(), e);
    }

    response.setHeader("Expires", "0");
    response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
    response.setHeader("Pragma", "public");

    response.setContentType("application/pdf");
    response.setContentLength(document.length);
    ServletOutputStream out = response.getOutputStream();
    out.write(document);
    out.flush();
}

From source file:it.jugpadova.controllers.JCaptchaController.java

@RequestMapping
public void image(HttpServletRequest req, HttpServletResponse res) throws IOException {
    byte[] captchaChallengeAsJpeg = null;
    // the output stream to render the captcha image as jpeg into
    ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();

    // get the session id that will identify the generated captcha.
    //the same id must be used to validate the res, the session id is a good candidate!
    String captchaId = req.getSession().getId();

    // call the ImageCaptchaService getChallenge method
    BufferedImage challenge = captchaService.getImageChallengeForID(captchaId, req.getLocale());

    // a jpeg encoder
    JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);
    jpegEncoder.encode(challenge);/*from   www .  java 2  s .  c  o  m*/

    captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

    // flush it in the res
    res.setHeader("Cache-Control", "no-store");
    res.setHeader("Pragma", "no-cache");
    res.setDateHeader("Expires", 0);
    res.setContentType("image/jpeg");
    ServletOutputStream resOutputStream = res.getOutputStream();
    resOutputStream.write(captchaChallengeAsJpeg);
    resOutputStream.flush();
    resOutputStream.close();
}

From source file:be.fedict.eid.applet.service.VcardServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    LOG.debug("doGet");

    HttpSession httpSession = request.getSession();
    EIdData eIdData = (EIdData) httpSession.getAttribute("eid");

    byte[] document;
    try {/*ww  w  .ja v  a 2s .  co  m*/
        document = this.vcardGenerator.generateVcard(eIdData);
    } catch (IOException e) {
        throw new ServletException("vCard generator error: " + e.getMessage(), e);
    }

    response.setHeader("Expires", "0");
    response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
    response.setHeader("Pragma", "public");

    response.setContentType(VcardLight.MIME_TYPE);
    response.setContentLength(document.length);
    ServletOutputStream out = response.getOutputStream();
    out.write(document);
    out.flush();
}

From source file:com.octo.captcha.module.acegi.JCaptchaImageController.java

public ModelAndView handleRequest(HttpServletRequest httpServletRequest, HttpServletResponse response)
        throws Exception {
    byte[] captchaChallengeAsJpeg = null;

    ByteArrayOutputStream jpegOutputStream = new ByteArrayOutputStream();

    //get the session id that will identify the generated captcha.
    //the same id must be used to validate the response, the session id is a good candidate!
    String captchaId = httpServletRequest.getSession().getId();

    BufferedImage challenge = imageCaptchaService.getImageChallengeForID(captchaId,
            httpServletRequest.getLocale());

    JPEGImageEncoder jpegEncoder = JPEGCodec.createJPEGEncoder(jpegOutputStream);
    jpegEncoder.encode(challenge);//  w ww  .  ja  v  a  2s  .  c o  m

    captchaChallengeAsJpeg = jpegOutputStream.toByteArray();

    // configure cache  directives
    response.setHeader("Cache-Control", "no-store");
    response.setHeader("Pragma", "no-cache");
    response.setDateHeader("Expires", 0);
    //flush content in the response
    response.setContentType("image/jpeg");
    ServletOutputStream responseOutputStream = response.getOutputStream();
    responseOutputStream.write(captchaChallengeAsJpeg);
    responseOutputStream.flush();
    responseOutputStream.close();
    return null;
}

From source file:q4.java

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
 * response)/* w  w w . j a va 2 s  . c o  m*/
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    HTableInterface table;
    StringBuilder sb = new StringBuilder();
    sb.append(TEAM);
    table = connection.getTable(tableName);
    int m = Integer.parseInt(request.getParameter("m"));
    int n = Integer.parseInt(request.getParameter("n"));

    byte[] location = Bytes.toBytes(request.getParameter("location").hashCode());
    byte[] date = Bytes.toBytes(Integer.parseInt(request.getParameter("date").replace("-", "")));
    try {
        ArrayList<Row> batchJob = new ArrayList<>();
        for (int i = m; i <= n; i++) {
            batchJob.add(new Get(Bytes.add(location, date, Bytes.toBytes(i))));
        }
        Object[] result = table.batch(batchJob);
        for (Object r : result) {
            if (r instanceof Result) {
                if (((Result) r).value() != null) {
                    //System.out.println(new String(((Result)r).value()));
                    sb.append(StringEscapeUtils.unescapeJson(new String(((Result) r).value()))).append("\n");
                }
            }
        }
    } catch (InterruptedException ex) {
    }

    ServletOutputStream out = response.getOutputStream();
    out.write(sb.toString().getBytes());
}