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.liusoft.dlog4j.action.ActionBase.java

/**
 * ??//from   www  .  j  av  a2  s  . c  o m
 * @param res
 * @param msg
 * @throws IOException
 */
protected static void outputPlainMsg(HttpServletResponse res, String msg) throws IOException {
    res.setContentType("text/plain;charset=UTF-8");
    res.getWriter().print(msg);
}

From source file:edu.harvard.med.screensaver.ui.arch.util.JSFUtils.java

/**
 * Performs the necessary steps to return server-side data, provided as an
 * InputStream, to an HTTP client. Must be called within a JSF-enabled servlet
 * environment./*from   w  w w.  j a v a2 s  . c  o  m*/
 *
 * @param facesContext the JSF FacesContext
 * @param dataInputStream an InputStream containing the data to send to the HTTP client
 * @param contentLocation set the "content-location" HTTP header to this value, allowing the downloaded file to be named
 * @param mimeType the MIME type of the file being sent
 * @throws IOException
 */
public static void handleUserDownloadRequest(FacesContext facesContext, InputStream dataInputStream,
        String contentLocation, String mimeType) throws IOException {
    HttpServletResponse response = (HttpServletResponse) facesContext.getExternalContext().getResponse();
    if (mimeType == null && contentLocation != null) {
        mimeType = MimetypesFileTypeMap.getDefaultFileTypeMap().getContentType(contentLocation);
    }
    if (mimeType != null) {
        response.setContentType(mimeType);
    }

    // NOTE: the second line does the trick with the filename. leaving first line in for posterity
    response.setHeader("Content-Location", contentLocation);
    response.setHeader("Content-disposition", "attachment; filename=\"" + contentLocation + "\"");

    OutputStream out = response.getOutputStream();
    IOUtils.copy(dataInputStream, out);
    out.close();

    // skip Render-Response JSF lifecycle phase, since we're generating a
    // non-Faces response
    facesContext.responseComplete();
}

From source file:com.yiji.openapi.sdk.util.Servlets.java

public static void writeResponse(HttpServletResponse response, String data) {
    OutputStream output = null;/*from   ww  w .j  av  a  2 s .c o m*/
    InputStream input = null;
    try {
        response.setCharacterEncoding("UTF-8");
        response.setContentType(MediaType.APPLICATION_JSON_VALUE);
        output = response.getOutputStream();
        input = new ByteArrayInputStream(data.getBytes(Charset.forName("UTF-8")));
        IOUtils.copy(input, output);
        output.flush();
    } catch (Exception e) {
        throw new RuntimeException("?(flushResponse):" + e.getMessage());
    } finally {
        IOUtils.closeQuietly(output);
        IOUtils.closeQuietly(input);
    }
}

From source file:net.sourceforge.ajaxtags.servlets.AjaxActionHelper.java

/**
 * Invoke the AJAX action and setup the request and response.
 *
 * @param action//from ww w . j  av a 2  s.co m
 *            the BaseAjaxXmlAction implementation
 * @param request
 *            the request
 * @param response
 *            the response
 * @return the XML content from action
 * @throws ServletException
 *             for any errors
 */
public static String invoke(final BaseAjaxXmlAction action, final HttpServletRequest request,
        final HttpServletResponse response) throws ServletException {
    // prepare CALL
    try {
        request.setCharacterEncoding(action.getXMLEncoding());
        // we will use UTF-8
    } catch (UnsupportedEncodingException e) {
        throw new ServletException(e);
    }
    // Set content to XML
    response.setContentType("text/xml; charset=" + action.getXMLEncoding());

    // enable the ajaxheaders
    for (HTMLAjaxHeader header : HTMLAjaxHeader.values()) {
        header.enable(response);
    }

    try {
        return action.getXmlContent(request, response);
    } catch (IOException e) {
        throw new ServletException(e);
    }
}

From source file:cn.vlabs.umt.ui.servlet.AddClientServlet.java

public static void writeJSONObject(HttpServletResponse response, Object object) {
    PrintWriter writer = null;//from w w  w  . j  a  v  a 2 s  . c  o  m
    try {
        //IE???text/html?
        response.setContentType("text/html");
        writer = response.getWriter();
        writer.write(object.toString());
    } catch (IOException e) {
    } finally {
        if (writer != null) {
            writer.flush();
            writer.close();
        }
    }
}

From source file:org.openmrs.module.omodexport.util.OmodExportUtil.java

/**
 * Utility module for exporting all modules. The exported modules will be in a zip file called
 * "modules.zip"/* w w  w. j a v  a 2 s .c om*/
 * 
 * @param response
 * @throws IOException
 */
public static void exportAllModules(HttpServletResponse response) throws IOException {

    Collection<Module> modules = ModuleFactory.getStartedModules();
    List<File> files = new ArrayList<File>();
    for (Module module : modules) {
        files.add(module.getFile());
    }
    response.setContentType("application/zip");
    response.setHeader("Content-Disposition", "attachment; filename=\"modules.ZIP\"");

    ZipOutputStream out = new ZipOutputStream(response.getOutputStream());

    zipFiles(files, out);
}

From source file:memedb.httpd.MemeDBHandler.java

public static void sendError(HttpServletResponse response, String errMsg, String reason, JSONObject status,
        int statusCode, Logger log) throws IOException {
    response.setStatus(statusCode);/*from   w w w.  j  a v  a  2s.c o  m*/
    response.setContentType(TEXT_PLAIN_MIMETYPE);
    try {
        new JSONWriter(response.getWriter()).object().key("ok").value(false).key("error").value(errMsg)
                .key("reason").value(reason).key("status").value(status).endObject();
    } catch (JSONException e) {
        if (log != null)
            log.error(e);
        throw new IOException(e);
    }
}

From source file:com.liusoft.dlog4j.action.ActionBase.java

/**
 * ??/*from w  w  w. j a v a  2s. c om*/
 * @param mapping
 * @param form
 * @param request
 * @param response
 * @param msg
 * @return
 * @throws IOException 
 */
protected static ActionForward msgbox(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response, String msg) throws IOException {
    response.setContentType("text/plain;charset=UTF-8");
    response.getWriter().print(msg);
    return null;
}

From source file:com.soolr.core.web.Servlets.java

public static void output(HttpServletResponse response, String contentType, Object content) throws IOException {
    ServletOutputStream output = response.getOutputStream();
    try {/*  w w w . j  a va 2 s  .co  m*/
        response.setContentType(contentType);
        if (content instanceof byte[]) {
            byte[] temp = (byte[]) content;
            output.write(temp);
        } else if (content instanceof String) {
            output.print(String.valueOf(content));
        }
    } finally {
        output.flush();
        output.close();
    }
}

From source file:co.edu.UNal.ArquitecturaDeSoftware.Bienestar.Vista.App.Usuario.Read.java

protected static void obtenerInscritosConv(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    int numC = CtrlUsuario.obtenerInscritosConv(Integer.parseInt(request.getParameter("1")));
    response.setContentType("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    JSONObject obj = new JSONObject();
    obj.put("inscritos", numC);
    out.print(obj);/*from ww  w. j  av a  2  s .  c om*/
}