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.jmu.util.ResponseJsonUtils.java

public static void writeJSON(ServletResponse response, String json) {
    try {/* w ww .  jav  a 2  s .co m*/
        response.setContentType("application/json; charset=UTF-8");
        writeJSON(response.getWriter(), json);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.jmu.util.ResponseJsonUtils.java

public static void writeJSON(ServletResponse response, AjaxResponse json) {
    try {/* ww  w  . j  a va  2s.c o  m*/
        response.setContentType("application/json; charset=UTF-8");
        writeJSON(response.getWriter(), json);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:com.tek271.reverseProxy.servlet.ContentUtils.java

public static void copyText(String text, ServletResponse response, ContentType contentType) {
    //response.setCharacterEncoding(contentType.charset);
    response.setContentType(contentType.value);
    try {/*from  w w w  .jav a 2 s. c o m*/
        response.getWriter().print(text);
    } catch (IOException e) {
        Throwables.propagate(e);
    }
}

From source file:org.apache.hadoop.util.ServletUtil.java

/**
 * Initial HTML header//from  w  w  w  .j  a  v  a  2  s.  c  o m
 */
public static PrintWriter initHTML(ServletResponse response, String title) throws IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<html>\n" + "<link rel='stylesheet' type='text/css' href='/static/hadoop.css'>\n" + "<title>"
            + title + "</title>\n" + "<body>\n" + "<h1>" + title + "</h1>\n");
    return out;
}

From source file:br.com.bancooriginal.notificacao.servlet.SampleServletApplication.java

@SuppressWarnings("serial")
@Bean/*from   w  w  w  .  ja  v a2 s.c o m*/
public Servlet dispatcherServlet() {
    return new GenericServlet() {
        @Override
        public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
            res.setContentType("text/plain");
            res.getWriter().append("Hello World");
        }
    };
}

From source file:MyServlet.java

public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();

    out.println("Init Parameters:");
    Enumeration e = getInitParameterNames();
    while (e.hasMoreElements()) {
        String name = (String) e.nextElement();
        out.println(name + ": " + getInitParameter(name));
    }/* w ww.  j a va  2 s .  c  o  m*/
}

From source file:GetKey.java

public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();

    String key = getInitParameter("key");
    String host = req.getServerName();
    int port = req.getServerPort();

    if (!keyFitsServer(key, host, port)) {
        out.println("Pirated!");
    } else {/*from  www. java  2  s  .  c  o  m*/
        out.println("Valid");
    }
}

From source file:ServerSnoop.java

public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();

    ServletContext context = getServletContext();
    out.println("req.getServerName(): " + req.getServerName());
    out.println("req.getServerPort(): " + req.getServerPort());
    out.println("context.getServerInfo(): " + context.getServerInfo());
    out.println("getServerInfo() name: " + getServerInfoName(context.getServerInfo()));
    out.println("getServerInfo() version: " + getServerInfoVersion(context.getServerInfo()));
    out.println("context.getAttributeNames():");
    Enumeration e = context.getAttributeNames();
    while (e.hasMoreElements()) {
        String name = (String) e.nextElement();
        out.println("  context.getAttribute(\"" + name + "\"): " + context.getAttribute(name));
    }/*  w w  w .  j  a v  a 2  s  . c  o  m*/
}

From source file:info.magnolia.cms.filters.ContentTypeFilter.java

private void setContentType(ServletRequest req, ServletResponse resp) {
    resp.setContentType(MIMEMapping.getMIMEType((HttpServletRequest) req));
    String characterEncoding = MIMEMapping.getContentEncoding((HttpServletRequest) req);

    if (StringUtils.isEmpty(characterEncoding)) {
        characterEncoding = "UTF-8"; //$NON-NLS-1$
    }//from w w w .ja  va2s  .  c  o  m

    resp.setCharacterEncoding(characterEncoding);

    try {
        req.setCharacterEncoding(characterEncoding);
    } catch (IllegalStateException e) {
        log.debug("can't set character encoding for the request", e); //$NON-NLS-1$
    } catch (UnsupportedEncodingException e) {
        log.error("can't set character encoding for the request", e); //$NON-NLS-1$
    }
}

From source file:net.javacrumbs.test.AbstractServlet.java

/**
 * Copies backend response to servlet response.
 *
 * @param response//  w  w  w  .  j  a  v a2 s  .  co  m
 * @param resp
 * @throws IOException
 */
protected void copyResultToResponse(HttpResponse response, ServletResponse resp) throws IOException {
    resp.setContentType(CONTENT_TYPE);
    final ServletOutputStream outputStream = resp.getOutputStream();
    response.getEntity().writeTo(outputStream);
}