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:bentolor.grocerylist.JsonResultRenderer.java

@Override
public void render(ActionRequest request, JsonResult jsonResult) throws Exception {
    HttpServletResponse response = request.getHttpServletResponse();
    response.setContentType(MimeTypes.MIME_APPLICATION_JSON);

    if (jsonResult.getStatus() > 0) {
        response.setStatus(jsonResult.getStatus());
    }//  ww  w.  ja va 2 s .c o m

    PrintWriter writer = null;
    try {
        writer = response.getWriter();
        String result;
        try {
            result = objectMapper.writeValueAsString(jsonResult.getModel());
        } catch (JsonProcessingException e) {
            throw new IllegalArgumentException("Invalid JSON to render");
        }
        writer.println(result);
    } finally {
        StreamUtil.close(writer);
    }
}

From source file:IncludeServlet.java

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

    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();

    out.println("<html>");
    out.println("<head>");
    out.println("<title>Include Servlet</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1>Welcome To Our Universe</h1>");
    out.println("Imagine the rest of the page here.<br><br>");
    //Include the copyright information
    RequestDispatcher dispatcher = request.getRequestDispatcher("/CopyRight");
    dispatcher.include(request, response);

    out.println("</body>");
    out.println("</html>");

    out.close();/*from ww w.j a  v a 2  s.  co  m*/

}

From source file:com.proteanplatform.web.core.mvc.JsonView.java

@Override
public void render(Map<String, ?> model, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    response.setContentType(getContentType());

    ObjectMapper mapper = new ObjectMapper();
    JsonFactory factory = mapper.getFactory();
    JsonGenerator json = null;//w  w  w  . j a va 2  s.c o  m

    try {
        json = factory.createGenerator(response.getWriter());
        json.writeObject(model);
    } finally {
        if (json != null) {
            json.close();
        }
    }
}

From source file:org.opensafety.hishare.view.ByteOutput.java

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    byte[] data = (byte[]) model.get("bytes");

    response.setContentType("application/octet-stream");
    response.getOutputStream().write(data);
    response.getOutputStream().flush();/*w w w. ja  v  a 2s .  c o  m*/
}

From source file:es.itecban.deployment.executionmanager.web.view.ReportActivityView.java

public void render(Map model, HttpServletRequest req, HttpServletResponse resp) throws Exception {
    PrintWriter out = resp.getWriter();

    resp.setContentType("application/octet-stream");

    ActivityExecReportType report = (ActivityExecReportType) model.get("report");
    out.print(report.getTechnicalActivityExecReport());
    out.flush();//from  w w  w. j  av a2  s. c  o m
    out.close();

}

From source file:org.motechproject.server.demo.web.CallMeController.java

@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setContentType("text/html");
    response.setCharacterEncoding("UTF-8");

    String phoneNumber = request.getParameter("phone");
    int delay = Integer.parseInt(request.getParameter("callDelay"));

    Calendar now = Calendar.getInstance();
    now.add(Calendar.MINUTE, delay);
    Date callTime = now.getTime();

    ModelAndView mav = new ModelAndView();

    demoService.schedulePhoneCall(phoneNumber, callTime);

    mav.setViewName("successView");

    return mav;//from  w  ww .j  av  a 2  s. c  o  m
}

From source file:WebAppProperties.java

public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType("text/html");
    PrintWriter out = resp.getWriter();
    ServletContext context = getServletContext();
    String displayName = context.getServletContextName();
    if (displayName == null) {
        displayName = "(no display-name element defined)";
    }//from   w  w  w.  ja v a 2 s .c o  m

    out.println("<html>");
    out.println("<body>");
    out.println("<br>Name: " + displayName);
    out.println("<br>Context: " + req.getContextPath());

    out.println("<h2><center>");
    out.println("Initialization Parameters</center></h2>");
    out.println("<br>");

    out.println("<center><table>");

    Enumeration e = context.getInitParameterNames();

    while (e.hasMoreElements()) {
        String name = (String) e.nextElement();
        out.println("<tr>");
        out.println("<td>" + name + "</td>");
        out.println("<td>" + context.getInitParameter(name) + "</td>");
        out.println("</tr>");
    }
    out.println("</table></center>");

    out.println("</body>");
    out.println("</html>");
    out.flush();
}

From source file:cf.spring.HealthzHandlerMapping.java

public HealthzHandlerMapping(int order) {
    setOrder(order);/*from  ww  w. j  av  a  2  s . c  o  m*/

    registerHandler("/healthz", new HttpRequestHandler() {
        @Override
        public void handleRequest(HttpServletRequest httpServletRequest,
                HttpServletResponse httpServletResponse) throws ServletException, IOException {
            httpServletResponse.setContentType("text/plain;charset=utf-8");
            httpServletResponse.getWriter().write("ok");
        }
    });
}

From source file:com.suntek.gztpb.controller.ChangeCarControll.java

@RequestMapping(value = "saveCarInfo.htm", method = RequestMethod.POST)
public @ResponseBody String saveTest(CarChangeModel carInfo, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    response.setContentType("text/html");
    response.setCharacterEncoding("utf-8");
    PrintWriter out = response.getWriter();

    String applyNum = IdGenerator.getInstance().getBizCode("ITMS_SEQ", 5);
    carInfo.setApplynum(applyNum);/*  w w w . ja  v a 2 s .c  om*/

    carInfo.setCreatedtime(new Date());
    carInfo.setFirstregdate(new Date());
    carInfo.setFinish(0);
    carInfo.setSource(0);
    carInfo.setSubmittime(new Date());

    carInfo.setCreator(request.getParameter("ownername"));

    try {
        carChangeService.signUp(carInfo);
        out.write("<script>parent.saveCallback(1,'" + applyNum + "')</script>");
    } catch (Exception e) {
        e.printStackTrace();
        out.write("<script>parent.saveCallback(0)</script>");
    }
    return null;
}

From source file:es.itecban.deployment.executionmanager.web.view.ReportView.java

public void render(Map model, HttpServletRequest req, HttpServletResponse resp) throws Exception {
    PrintWriter out = resp.getWriter();

    resp.setContentType("application/octet-stream");

    ExecutionReportType report = (ExecutionReportType) model.get("report");
    //      report.getTechnicalExecutionReport();
    out.print(report.getTechnicalExecutionReport());
    out.flush();/*from w w  w  .  jav  a  2s  .  c o m*/
    out.close();

}