Example usage for javax.servlet ServletResponse setContentType

List of usage examples for javax.servlet ServletResponse setContentType

Introduction

In this page you can find the example usage for javax.servlet ServletResponse 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.netsteadfast.greenstep.base.filter.CharsetFilter.java

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    response.setContentType(this.contentType);
    response.setCharacterEncoding(this.encoding);
    chain.doFilter(request, response);/*from ww  w  .  ja  va  2s. co  m*/
}

From source file:com.autentia.intra.filter.UTF8Filter.java

/**
 * Metemos en la request el formato de codificacion UTF-8
 *//*w  w w .j av a  2s.c  om*/
public void doFilter(ServletRequest request, ServletResponse response, FilterChain fc)
        throws IOException, ServletException {
    response.setContentType(contentType);
    request.setCharacterEncoding(encoding);
    fc.doFilter(request, response);
}

From source file:com.thoughtworks.go.server.web.BackupFilter.java

private void generateResponseForIsBackupFinishedAPI(ServletResponse response) {
    response.setContentType("application/json");
    JsonObject json = new JsonObject();
    json.addProperty("is_backing_up", backupService.isBackingUp());
    try {//w  ww.  j  av  a 2s . c o  m
        response.getWriter().print(json);
    } catch (IOException e) {
        LOGGER.error("General IOException: {}", e.getMessage());
    }
}

From source file:MyServlet.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {

    chain.doFilter(request, response);/*from  w  w w .j a va  2  s . co  m*/
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("The filter got this message: " + filterConfig.getInitParameter("message"));
}

From source file:org.ng200.openolympus.controller.auth.RecaptchaAuthenticationFilter.java

private void recaptchaError(ServletRequest request, ServletResponse response, List<String> recaptchaErrors)
        throws IOException {

    response.setContentType("application/json");
    AuthenticationResponder.writeLoginStatusJson(response.getWriter(), "unknown", recaptchaErrors);
}

From source file:com.iisigroup.cap.component.impl.FileDownloadResult.java

public void respondResult(ServletResponse response) {
    InputStream in = null;/*  w  ww  .j  av  a2s .  c  o  m*/
    OutputStream output = null;
    try {
        response.setContentType(_contentType);
        if (_outputName != null && response instanceof HttpServletResponse) {
            HttpServletResponse resp = (HttpServletResponse) response;
            resp.setHeader("Content-Disposition", "attachment;filename=\"" + _outputName + "\"");
            resp.setHeader("Cache-Control", "public");
            resp.setHeader("Pragma", "public");
        }
        output = response.getOutputStream();
        File file = new File(_file);
        int length = -1;
        // Stream to the requester.
        byte[] bbuf = new byte[1024 * 1024];
        int len = 0;
        in = new FileInputStream(file);
        while ((in != null) && ((length = in.read(bbuf)) != -1)) {
            output.write(bbuf, 0, length);
            len += length;
        }
        response.setContentLength(len);
        output.flush();
    } catch (Exception e) {
        throw new CapException(e, getClass());
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(output);
    }

}

From source file:edu.cornell.mannlib.vitro.webapp.filters.CharsetEncodingFilter.java

private void setContentTypeOnResponse(HttpServletRequest req, ServletResponse servletResponse) {
    if (servletResponse.getContentType() != null) {
        return;//from  w ww.jav  a  2s .  co m
    }

    String uri = req.getRequestURI();
    if (!hasExtension(uri)) {
        servletResponse.setContentType("text/html;charset=UTF-8");
        return;
    }

    String typeFromContext = req.getSession().getServletContext().getMimeType(uri);
    if (typeFromContext == null) {
        servletResponse.setContentType("text/html;charset=UTF-8");
        return;
    }

    servletResponse.setContentType(typeFromContext);
    servletResponse.setCharacterEncoding("UTF-8");
}

From source file:com.iisigroup.cap.component.impl.ByteArrayDownloadResult.java

@Override
public void respondResult(ServletResponse response) {
    int length = -1;
    InputStream in = null;// w  ww  .j ava 2  s  . c om
    OutputStream output = null;
    try {
        response.setContentType(getContentType());
        response.setContentLength(_byteArray.length);
        if (getOutputName() != null && response instanceof HttpServletResponse) {
            HttpServletResponse resp = (HttpServletResponse) response;
            HttpServletRequest req = (HttpServletRequest) _request.getServletRequest();
            String userAgent = req.getHeader("USER-AGENT");
            if (StringUtils.contains(userAgent, "MSIE")) {
                resp.setHeader("Content-Disposition", "attachment; filename=\"" + _outputName + "\"");
            } else {
                resp.setHeader("Content-Disposition", "attachment; filename*=UTF-8''" + _outputName);
            }
            resp.setHeader("Cache-Control", "public");
            resp.setHeader("Pragma", "public");
        }
        output = response.getOutputStream();
        // Stream to the requester.
        byte[] bbuf = new byte[1024 * 1024];

        in = new ByteArrayInputStream(_byteArray);
        while ((in != null) && ((length = in.read(bbuf)) != -1)) {
            output.write(bbuf, 0, length);
        }
        output.flush();
    } catch (IOException e) {
        e.getMessage();
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(output);
    }
}

From source file:org.overlord.commons.ui.header.OverlordHeaderResources.java

/**
 * Serve the appropriate resource's binary content.
 * @param response//  w w w. java 2 s  .  c o m
 * @param resourceURL
 * @throws IOException 
 */
private void serveResource(ServletResponse response, URL resourceURL) throws IOException {
    String extension = resourceURL.getPath().substring(resourceURL.getPath().lastIndexOf('.') + 1);
    String ct = FILE_TYPES.get(extension);
    response.setContentType(ct);

    InputStream in = null;
    OutputStream out = null;
    try {
        in = resourceURL.openStream();
        out = response.getOutputStream();
        IOUtils.copy(in, out);
    } finally {
        IOUtils.closeQuietly(in);
        IOUtils.closeQuietly(out);
    }

}

From source file:MyServlet.java

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
    String password = ((HttpServletRequest) request).getParameter("password");

    if (password.equals("opensesame")) {
        chain.doFilter(request, response);
    } else {//from www . j  a  v a  2s. com
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        out.println("<HTML>");
        out.println("<HEAD>");
        out.println("<TITLE>");
        out.println("Incorrect Password");
        out.println("</TITLE>");
        out.println("</HEAD>");
        out.println("<BODY>");
        out.println("<H1>Incorrect Password</H1>");
        out.println("Sorry, that password was incorrect.");
        out.println("</BODY>");
        out.println("</HTML>");
    }
}