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:org.vbossica.springbox.metrics.MetricsController.java

@RequestMapping(value = "/metrics/metrics", method = RequestMethod.GET)
public void process(
        @RequestParam(value = "pretty", required = false, defaultValue = "true") boolean prettyPrint,
        HttpServletResponse resp) throws IOException {
    resp.setContentType("application/json");
    resp.setHeader("Cache-Control", "must-revalidate,no-cache,no-store");
    resp.setStatus(HttpServletResponse.SC_OK);

    final OutputStream output = resp.getOutputStream();
    try {//from   w w  w  .j a v  a  2s  .  c o m
        (prettyPrint ? mapper.writerWithDefaultPrettyPrinter() : mapper.writer()).writeValue(output, registry);
    } finally {
        output.close();
    }
}

From source file:LoginHandler.java

public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    String account = req.getParameter("account");
    String password = req.getParameter("password");
    String pin = req.getParameter("pin");

    if (!allowUser(account, password, pin)) {
        out.println("<HTML><HEAD><TITLE>Access Denied</TITLE></HEAD>");
        out.println("<BODY>Your login and password are invalid.<BR>");
        out.println("You may want to <A HREF=\"/login.html\">try again</A>");
        out.println("</BODY></HTML>");
    } else {//from   ww w  .j  ava 2 s  .c o m
        // Valid login. Make a note in the session object.
        HttpSession session = req.getSession();
        session.setAttribute("logon.isDone", account);
        // Try redirecting the client to the page he first tried to access
        try {
            String target = (String) session.getAttribute("login.target");
            if (target != null) {
                res.sendRedirect(target);
                return;
            }
        } catch (Exception ignored) {
        }

        // Couldn't redirect to the target. Redirect to the site's home page.
        res.sendRedirect("/");
    }
}

From source file:UrlRewrite.java

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

    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    String contextPath = request.getContextPath();
    String encodedUrl = response.encodeURL(contextPath + "/default.jsp");

    out.println("<html>");
    out.println("<head>");
    out.println("<title>URL Rewriter</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>This page will use URL rewriting if necessary</h2>");
    out.println("Go to the default.jsp page <a href=\"" + encodedUrl + "\">here</a>.");
    out.println("</body>");
    out.println("</html>");

}

From source file:org.openmrs.module.clinicalsummary.web.controller.watcher.DownloadUploadWatcherController.java

@RequestMapping(method = RequestMethod.GET)
public void watch(final HttpServletResponse response) throws IOException {
    response.setContentType(MimeType.APPLICATION_JSON);
    JsonFactory factory = new JsonFactory();
    JsonGenerator generator = factory.createJsonGenerator(response.getOutputStream(), JsonEncoding.UTF8);
    generator.useDefaultPrettyPrinter();

    generator.writeStartObject();/*from   w ww.  j av a 2s.co  m*/

    SummariesTaskInstance instance = SummariesTaskInstance.getInstance();
    generator.writeBooleanField("running", instance.isRunning());
    generator.writeStringField("filename", instance.getCurrentFilename());
    generator.writeStringField("task", instance.getCurrentTask());

    String summariesFilename = instance.getSummariesFilename();
    if (!instance.isRunning() && StringUtils.isNotEmpty(summariesFilename)) {
        generator.writeStringField("file", summariesFilename);
        SummariesTaskInstance.removeInstance();
    }

    generator.writeEndObject();
    generator.close();
}

From source file:io.v.positioning.BluetoothProximityServlet.java

/**
 * Processes a POST request to save nearby devices scanned using Bluetooth
 *
 * @param req  The servlet request that includes JSONObject with properties to be saved
 * @param resp The servlet response informing a user if the request was successful or not
 *///ww w.ja  v a 2 s. co  m
@Override
public void doPost(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/plain");
    BufferedReader br = req.getReader();
    try {
        JSONObject jo = new JSONObject(br.readLine());
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        Entity entity = new Entity("bluetooth");
        entity.setProperty("androidId", jo.get("androidId"));
        entity.setProperty("myMacAddress", jo.get("myMacAddress"));
        entity.setProperty("remoteName", jo.get("remoteName"));
        entity.setProperty("remoteAddress", jo.get("remoteAddress"));
        entity.setProperty("remoteRssi", jo.get("remoteRssi"));
        entity.setProperty("deviceTime", jo.get("deviceTime"));
        entity.setProperty("serverTime", System.currentTimeMillis());
        datastore.put(entity);
        resp.getWriter().println("New bluetooth device added.");
    } catch (Exception e) {
        resp.getWriter().println("Recording failed. " + e);
    }
}

From source file:MainServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    count++;//from  ww  w  . ja  va2  s  . co  m
    out.println("Since the beginning, this servlet has been accessed " + count + " times.");
}

From source file:com.crunchify.jsp.servlet.BarServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.setContentType("image/png");
    OutputStream outputStream = response.getOutputStream();
    JFreeChart chart = getChart();//from  ww w.  ja  v  a 2  s. c  o  m
    int width = 500;
    int height = 350;
    ChartUtilities.writeChartAsPNG(outputStream, chart, width, height);

}

From source file:org.dspace.app.webui.cris.controller.RSSStatController.java

public ModelAndView daily(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
    arg1.setContentType("application/rss+xml");
    processRSSStat(arg0, StatSubscription.FREQUENCY_DAILY).output(arg1.getWriter());
    arg1.getWriter().flush();/*from w w  w.ja v  a  2s .co m*/
    return null;
}

From source file:org.dspace.app.webui.cris.controller.RSSStatController.java

public ModelAndView weekly(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
    arg1.setContentType("application/rss+xml");
    processRSSStat(arg0, StatSubscription.FREQUENCY_WEEKLY).output(arg1.getWriter());
    arg1.getWriter().flush();/* ww  w  . j  a v  a 2s . co m*/
    return null;
}

From source file:org.dspace.app.webui.cris.controller.RSSStatController.java

public ModelAndView monthly(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
    arg1.setContentType("application/rss+xml");
    processRSSStat(arg0, StatSubscription.FREQUENCY_MONTHLY).output(arg1.getWriter());
    arg1.getWriter().flush();/*from   w w w. j  a  v  a2s .c  om*/
    return null;
}