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.slience.controller.SolrController.java

@RequestMapping("/solr")
@ResponseBody/*from  w w  w .j  a  va 2  s  .c o m*/
public void handSolrRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    request.setCharacterEncoding("UTF-8");
    response.setContentType("application/json;charset=UTF-8");

    //solrSimpleQueryService.service();

}

From source file:org.craftercms.cstudio.publishing.servlet.StatusMonitoringServlet.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType(MimeTypeUtils.APPLICATION_JSON_VALUE);
    response.getWriter().println("{ status: \"" + statusMessage + "\" }");
    response.setStatus(HttpServletResponse.SC_OK);
}

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

/**
 * Processes a POST request to save device's GPS location
 *
 * @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
 *//*  w w w . j av a 2 s . c o  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("gps");
        entity.setProperty("androidId", jo.get("androidId"));
        entity.setProperty("latitude", jo.get("latitude"));
        entity.setProperty("longitude", jo.get("longitude"));
        entity.setProperty("deviceTime", jo.get("deviceTime"));
        entity.setProperty("serverTime", System.currentTimeMillis());
        datastore.put(entity);
        resp.getWriter().println("New GPS location added.");
    } catch (Exception e) {
        resp.getWriter().println("Recording failed. " + e.getMessage());
    }
}

From source file:MyServlet.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 {// ww w.  jav  a2  s.c  o m
        out.println("<HTML><HEAD><TITLE>Access Denied</TITLE></HEAD>");
        out.println("<BODY>Your login and password are valid.<BR>");
        out.println("</BODY></HTML>");
        HttpSession session = req.getSession();
        session.setAttribute("logon.isDone", account); // just a marker object

        try {
            String target = (String) session.getAttribute("login.target");
            if (target != null) {
                res.sendRedirect(target);
                return;
            }
        } catch (Exception ignored) {
        }

        //res.sendRedirect("/");
    }
}

From source file:MyServlet.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    out.println("<HTML>");
    out.println("<HEAD>");
    out.println("<TITLE>");
    out.println("User Authentication");
    out.println("</TITLE>");
    out.println("</HEAD>");
    out.println("<BODY>");
    out.println("<H1>User Authentication</H1>");

    String type = request.getAuthType();
    out.println("Welcome to this secure page.<BR>");
    out.println("Authentication mechanism: " + type + "<BR>");
    Principal principal = request.getUserPrincipal();
    out.println("Your username is: " + principal.getName() + "<BR>");

    out.println("</BODY>");
    out.println("</HTML>");
}

From source file:org.trimou.spring.web.view.TrimouView.java

@Override
protected void renderMergedTemplateModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    response.setContentType(getContentType());
    exposeRequestAttributesAsModel(model, request);
    final Writer writer = response.getWriter();
    try {//from   ww w .  j av a  2  s.  c  o m
        engine.getMustache(viewName).render(writer, model);
    } catch (NullPointerException e) {
        throw new MustacheException(getUrl() + " is not exist.", e);
    } finally {
        writer.flush();
    }
}

From source file:com.marosj.backend.BackendHttpServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("application/json");

    ObjectMapper mapper = new ObjectMapper();
    String greeting = req.getParameter("greeting");

    ResponseDTO response = new ResponseDTO();
    response.setGreeting(greeting + " from cluster Backend");
    response.setTime(System.currentTimeMillis());
    response.setIp(getIp());/*ww w.j  av a2s.c o  m*/

    PrintWriter out = resp.getWriter();
    mapper.writerWithDefaultPrettyPrinter().writeValue(out, response);
}

From source file:com.controlj.green.modstat.servlets.ModstatZipServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("application/zip");
    resp.setHeader("Content-disposition", "attachment;filename=modstat.zip");
    try {//  ww  w. ja v a  2 s .c om
        RunnableProgress work = (RunnableProgress) LongRunning.getWork(req);
        if (work == null || work.isAlive() || work.hasError()) {
            throw new Exception("Modstats not gathered yet");
        }

        InputStream in = work.getCache();
        IOUtils.copy(in, resp.getOutputStream());
        resp.getOutputStream().flush();
    } catch (Exception e) {
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.getMessage());
        System.err.println("Add-On Error in " + AddOnInfo.getAddOnInfo().getName());
        e.printStackTrace(new PrintWriter(System.err));
    }
}

From source file:net.hillsdon.reviki.web.pages.impl.TextFormatSearchResults.java

public void render(final HttpServletRequest request, final HttpServletResponse response) throws Exception {
    response.setContentType("text/plain");
    render(request, response.getWriter());
}

From source file:net.sf.appstatus.web.pages.AbstractPage.java

protected void setup(HttpServletResponse resp, String type) throws IOException {
    resp.setContentType(type);
    resp.setCharacterEncoding(ENCODING);
}