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:Buffering.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setBufferSize(8 * 1024); // 8K 
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    int size = res.getBufferSize(); // returns 8096 or greater

    log("The default buffer size is " + size);

    out.println("The client won't see this");
    res.reset();// ww  w .  j  ava  2 s  . c o  m
    out.println("Nor will the client see this!");
    res.reset();
    out.println("And this won't be seen if sendError() is called");
    if (req.getParameter("param") == null) {
        res.sendError(res.SC_BAD_REQUEST, "param needed");
    }
}

From source file:WebAppProperties.java

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();
    ServletContext context = getServletContext();
    String displayName = context.getServletContextName();
    if (displayName == null) {
        displayName = "(no display-name element defined)";
    }//from w  w w . j a v a 2  s. c  o  m

    out.println("<html>");
    out.println("<head>");
    out.println("<title>Web Application Properties");
    out.println("</title>");
    out.println("</head><body>");
    out.println("<h1>Web Application Properties</h2>");
    out.println("<br>Name: " + displayName);
    out.println("<br>Context: " + req.getContextPath());

    out.println("<h2><center>");
    out.println("Initialization Parameters</center></h2>");
    out.println("<br>");

    out.println("<center><table border width=80%>");

    Enumeration e = context.getInitParameterNames();

    while (e.hasMoreElements()) {
        String name = (String) e.nextElement();
        out.println("<tr>");
        out.println("<td>" + name + "</td>");
        out.println("<td>" + context.getInitParameter(name) + "</td>");
        out.println("</tr>");
    }
    out.println("</table></center>");

    out.println("</body>");
    out.println("</html>");
    out.flush();
}

From source file:com.zuoxiaolong.niubi.job.test.http.DownloadFileController.java

@RequestMapping("/download/test.txt")
public void downloadTxt(HttpServletResponse response) throws IOException {
    String fileName = "test.txt";
    response.setContentType("text/plain");
    response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
    OutputStream outputStream = response.getOutputStream();
    outputStream.write("hello".getBytes());
    outputStream.flush();/*from   w w  w  .j  a  v a2s. c  o  m*/
    outputStream.close();
}

From source file:com.zuoxiaolong.niubi.job.test.http.DownloadFileController.java

@RequestMapping("/download/test.jar")
public void downloadJar(HttpServletResponse response) throws IOException {
    String fileName = "test.jar";
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=" + fileName);
    OutputStream outputStream = response.getOutputStream();
    outputStream.write("hello".getBytes());
    outputStream.flush();/*from  w w  w.j a  v  a 2s .  c o  m*/
    outputStream.close();
}

From source file:MyServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setBufferSize(8 * 1024); // 8K buffer
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    int size = res.getBufferSize(); // returns 8096 or greater

    // Record the default size, in the log
    log("The default buffer size is " + size);

    out.println("The client won't see this");
    res.reset();/* w  ww . java  2 s  .c  o  m*/
    out.println("And this won't be seen if sendError() is called");
    if (req.getParameter("important_parameter") == null) {
        res.sendError(res.SC_BAD_REQUEST, "important_parameter needed");
    }
}

From source file:MyServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, java.io.IOException {

    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    HttpSession session = request.getSession();

    out.println("<html>");
    out.println("<head>");
    out.println("<title>Simple Session Tracker</title>");
    out.println("</head>");
    out.println("<body>");

    out.println("<h2>Session Info</h2>");
    out.println("session Id: " + session.getId() + "<br><br>");
    out.println("The SESSION TIMEOUT period is " + session.getMaxInactiveInterval() + " seconds.<br><br>");
    out.println("Now changing it to 20 minutes.<br><br>");
    session.setMaxInactiveInterval(20 * 60);
    out.println("The SESSION TIMEOUT period is now " + session.getMaxInactiveInterval() + " seconds.");

    out.println("</body>");
    out.println("</html>");

}

From source file:gumga.framework.presentation.api.AbstractReportAPI.java

protected void setContentType(HttpServletResponse response, String reportName, ReportType type) {
    response.setContentType(type.getContentType());
    response.setHeader("Content-disposition", "inline; filename=" + reportName);
}

From source file:org.jasig.portlet.maps.mvc.JsonContentTypeFilter.java

@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response,
        FilterChain filterChain) throws ServletException, IOException {
    response.setContentType("application/json");
    filterChain.doFilter(request, response);
}

From source file:com.example.multipart.MultipartServiceImpl.java

@Override
public void downloadByteArrayData(byte[] content, String contentType, String filename,
        HttpServletResponse response) throws IOException {

    InputStream is = new ByteArrayInputStream(content);

    response.setContentType(contentType);
    response.addHeader(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=\"" + filename + "\"");
    copy(is, response.getOutputStream());
    response.flushBuffer();/*from  w  w  w .  jav a2  s  .c  o m*/
}

From source file:de.knurt.fam.template.controller.json.PublicDocController.java

@Override
public ModelAndView handleRequest(HttpServletRequest rq, HttpServletResponse rs) {
    PrintWriter pw = null;//from   www  . j a  v  a2  s .co  m
    try {
        rs.setContentType("application/json");
        pw = rs.getWriter();
        String result = "{}";
        if (rq.getParameter("doc") != null && this.isAllowedToShow(rq, rq.getParameter("doc"))) {
            result = FamCouchDBDao.getInstance().getContentAsString(rq.getParameter("doc"));
        }
        IOUtils.write(result, pw);
    } catch (IOException ex) {
        FamLog.exception(ex, 201204191241l);
    } finally {
        IOUtils.closeQuietly(pw);
    }
    return null;
}