Example usage for javax.servlet.http HttpServletResponse setContentType

List of usage examples for javax.servlet.http HttpServletResponse setContentType

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse setContentType.

Prototype


public void setContentType(String type);

Source Link

Document

Sets the content type of the response being sent to the client, if the response has not been committed yet.

Usage

From source file:com.tvst.api.servlet.HomeServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from  ww  w  .  j a v  a  2  s  .  c  o  m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        JSONObject object = new JSONObject();
        object.put(JSONKey.status.toString(), 1);
        object.put(JSONKey.server_time.toString(), System.currentTimeMillis());
        out.println(object.toString());
    }
}

From source file:eu.europa.ejusticeportal.dss.controller.action.DownloadSealedPdf.java

/**
 * Send the PDF to the http response//from  w  ww  . j ava  2s . co  m
 * @param is the stream containing the PDF
 * @param pdfName the name of the PDF
 * @param response the HttpServletResponse to which the PDF will be written
 * @throws IOException
 */
private void sendPdf(InputStream is, String pdfName, HttpServletResponse response) throws IOException {
    response.setContentType("application/pdf");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + pdfName + "\"");

    ServletOutputStream outs = null;
    try {
        outs = response.getOutputStream();
        int r = 0;
        byte[] chunk = new byte[8192];
        while ((r = is.read(chunk)) != -1) {
            outs.write(chunk, 0, r);
        }
        outs.flush();
    } finally {
        IOUtils.closeQuietly(outs);
    }

}

From source file:controllers.CarOptionValueController.java

@RequestMapping("/getXls")
public void getXls(Map<String, Object> model, HttpServletResponse response) throws Exception {
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=options.xls");
    carOptionValueService.getXls().write(response.getOutputStream());
}

From source file:fr.putnami.pwt.plugin.ajaxbot.controller.SiteMapController.java

@RequestMapping(value = "/sitemap.txt", method = RequestMethod.GET)
public void welcomePage(HttpServletResponse response) {
    try {/*from   w  w  w  .ja  va2 s  . c om*/
        InputStream is = new FileInputStream(sitemap);
        response.setContentType("text/plain");
        response.setContentLength((int) sitemap.length());
        IOUtils.copy(is, response.getOutputStream());
        response.flushBuffer();
    } catch (IOException ex) {
        throw new RuntimeException("IOError writing file to output stream", ex);
    }
}

From source file:org.nsesa.editor.gwt.an.amendments.server.service.HtmlExportService.java

@Override
public void export(final AmendmentContainerDTO object, final HttpServletResponse response) throws IOException {
    response.setContentType("text/html; charset=UTF-8");
    response.setHeader("Content-Disposition",
            "attachment;filename=" + object.getAmendmentContainerID() + ".html");
    response.setCharacterEncoding("UTF8");

    final String content = object.getBody();
    final InputSource inputSource;
    inputSource = new InputSource(new StringReader(content));

    try {/*from   w w w.  j a  va2  s . c  o m*/
        final NodeModel model = NodeModel.parse(inputSource);
        final Configuration configuration = new Configuration();
        configuration.setDefaultEncoding("UTF-8");
        configuration.setDirectoryForTemplateLoading(template.getFile().getParentFile());
        final StringWriter sw = new StringWriter();
        Map<String, Object> root = new HashMap<String, Object>();
        root.put("amendment", model);
        root.put("editorUrl", editorUrl);
        configuration.getTemplate(template.getFile().getName()).process(root, sw);
        byte[] bytes = sw.toString().getBytes("utf-8");
        IOUtils.copy(new ByteArrayInputStream(bytes), response.getOutputStream());
        response.setContentLength(bytes.length);
        response.flushBuffer();

    } catch (IOException e) {
        throw new RuntimeException("Could not read file.", e);
    } catch (SAXException e) {
        throw new RuntimeException("Could not parse file.", e);
    } catch (ParserConfigurationException e) {
        throw new RuntimeException("Could not parse file.", e);
    } catch (TemplateException e) {
        throw new RuntimeException("Could not load template.", e);
    }
}

From source file:com.yunmel.syncretic.utils.io.IOUtils.java

/**
 * (??)// ww  w  .  jav a  2 s  .c  o m
 * 
 * @param response
 * @param is ?
 * @param realName ??
 */
public static void downloadFile(HttpServletResponse response, InputStream is, String realName) {
    BufferedInputStream bis = null;
    BufferedOutputStream bos = null;
    try {
        response.setContentType("text/html;charset=UTF-8");
        // request.setCharacterEncoding("UTF-8");
        long fileLength = is.available();

        response.setContentType("application/octet-stream");
        realName = new String(realName.getBytes("GBK"), "ISO8859-1");
        response.setHeader("Content-disposition", "attachment; filename=" + realName);
        response.setHeader("Content-Length", String.valueOf(fileLength));
        bis = new BufferedInputStream(is);
        bos = new BufferedOutputStream(response.getOutputStream());
        byte[] buff = new byte[2048];
        int bytesRead;
        while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
            bos.write(buff, 0, bytesRead);
        }
    } catch (Exception e) {
        // e.printStackTrace();//???
    } finally {
        try {
            bos.close();
            bis.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

From source file:de.knurt.fam.core.model.config.CronjobActionController.java

private ModelAndView pseudoResponse(HttpServletResponse rs) {
    PrintWriter pw = null;/*from   w  ww .  ja v  a  2 s  . co  m*/
    try {
        rs.setContentType("text/plain;charset=UTF-8");
        pw = rs.getWriter();
        IOUtils.write("done", pw);
    } catch (IOException ex) {
        FamLog.exception(ex, 200911182012l);
    } finally {
        IOUtils.closeQuietly(pw);
    }
    return null;
}

From source file:MyServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    try {//from   www  .  j a v  a  2s . c  o m
        res.setContentType("text/plain; charset=UTF-8");
        PrintWriter out = res.getWriter();

        String charset = req.getParameter("charset");

        String text = req.getParameter("text");

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(new StringBufferInputStream(text), charset));
        text = reader.readLine();

        out.println("Received charset: " + charset);
        out.println("Received text: " + text);
        out.println("Received text (escaped): " + toUnicodeEscapeString(text));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.craftercms.core.util.spring.mvc.GsonView.java

@Override
protected void prepareResponse(HttpServletRequest request, HttpServletResponse response) {
    response.setContentType(getContentType());
    response.setCharacterEncoding(DEFAULT_CHARACTER_ENCODING);
    if (disableCaching) {
        HttpServletUtils.disableCaching(response);
    }//from  w w w . ja  va  2s . co m
}

From source file:Counter.java

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    HttpSession session = req.getSession(true);
    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();
    int count = 1;
    Integer i = (Integer) session.getAttribute(COUNTER_KEY);
    if (i != null) {
        count = i.intValue() + 1;/* ww w .  ja  va  2  s.c o m*/
    }
    session.setAttribute(COUNTER_KEY, new Integer(count));
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Session Counter</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("Your session ID is <b>" + session.getId());
    out.println("</b> and you have hit this page <b>" + count + "</b> time(s) during this browser session");

    out.println("<form method=GET action=\"" + req.getRequestURI() + "\">");
    out.println("<input type=submit " + "value=\"Hit page again\">");
    out.println("</form>");
    out.println("</body>");
    out.println("</html>");
    out.flush();
}