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:ru.mystamps.web.controller.SitemapController.java

@GetMapping(Url.SITEMAP_XML)
public void getSitemapXml(HttpServletResponse response) {
    response.setContentType("application/xml");
    response.setCharacterEncoding("UTF-8");

    DateFormat dateFormatter = new SimpleDateFormat(DATE_FORMAT, Locale.ENGLISH);

    try {//  www  .  ja v  a 2  s  . c o m
        PrintWriter writer = response.getWriter();

        writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        writer.println("<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">");

        writer.print("\t<url>\t\t<loc>");
        writer.print(Url.PUBLIC_URL);
        writer.print(Url.INDEX_PAGE);
        writer.println("</loc>\t</url>");

        for (SitemapInfoDto item : seriesService.findAllForSitemap()) {
            writer.println("\t<url>");

            writer.print("\t\t<loc>");
            writer.print(createLocEntry(item));
            writer.println("</loc>");

            writer.print("\t\t<lastmod>");
            writer.print(createLastModEntry(dateFormatter, item));
            writer.println("</lastmod>");

            writer.println("\t</url>");
        }

        writer.println("</urlset>");
    } catch (IOException ex) {
        LOG.error("Can't return sitemap.xml: {}", ex.getMessage());
    }
}

From source file:com.WeatherProxy.java

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("application/json; charset=utf-8");
    resp.setHeader("Access-Control-Allow-Origin", "*");
    PrintWriter out = resp.getWriter();
    String wenhamLat = "42.589611";
    String wenhamLng = "-70.819806";
    // Retrieve lat and lng form url
    String lat = req.getParameter("lat");
    String lng = req.getParameter("lng");

    // Get weather data
    this.getWeather(lat, lng);

    out.println(weatherNow);//from  w  w w.  ja v  a 2s .  c  om
}

From source file:com.zuoxiaolong.blog.common.web.JsonHandlerMethodReturnValueHandler.java

@Override
public void handleReturnValue(Object returnValue, MethodParameter returnType,
        ModelAndViewContainer mavContainer, NativeWebRequest webRequest) throws Exception {
    mavContainer.setRequestHandled(true);
    HttpServletResponse httpServletResponse = webRequest.getNativeResponse(HttpServletResponse.class);
    httpServletResponse.setContentType(CONTENT_TYPE);
    ServletServerHttpResponse outputMessage = new ServletServerHttpResponse(httpServletResponse);
    JsonResponse jsonResponse = new JsonResponse(returnValue);
    outputMessage.getBody().write(StringUtils.toBytes(JsonUtils.toJson(jsonResponse)));
    outputMessage.getBody().flush();/*from   w  ww. j a  v a2s . c  o  m*/
}

From source file:architecture.ee.web.community.spring.controller.SecureDisplayController.java

protected void setContentType(HttpServletResponse response) {
    response.setContentType(DEFAULT_CONTENT_TYPE);
}

From source file:de.knurt.fam.template.controller.json.CouchPutController.java

@Override
public ModelAndView handleRequest(HttpServletRequest rq, HttpServletResponse rs) {
    PrintWriter pw = null;/*from  ww  w . j  a va 2s  . co m*/
    try {
        rs.setContentType("application/json");
        pw = rs.getWriter();
        String result = "{}";
        if (rq.getParameter("body") != null && this.isAllowedToShow(rq, rq.getParameter("body"))) {
            Response response = FamCouchDBDao.getInstance().put(rq.getParameter("body"));
            result = response != null ? response.getContentAsString() : "{error: \"unknown\"}";
        }
        IOUtils.write(result, pw);
    } catch (IOException ex) {
        FamLog.exception(ex, 201204191240l);
    } finally {
        IOUtils.closeQuietly(pw);
    }
    return null;
}

From source file:AreaCode.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/vnd.wap.wml");
    PrintWriter out = res.getWriter();

    String msg = null;//from   w  w  w . j a va2 s . c  om

    String code = req.getParameter("code");
    String region = null;
    if (code != null) {
        region = lookup.getProperty(code);
    }

    out.println("<?xml version=\"1.0\"?>");
    out.println("<!DOCTYPE wml PUBLIC " + "\"-//WAPFORUM//DTD WML 1.1//EN\" "
            + "\"http://www.wapforum.org/DTD/wml_1.1.xml\">");

    out.println("<wml>");
    out.println("<card id=\"Code\" title=\"Code\">");
    out.println("  <p>");
    out.println("  Area code '" + code + "'<br/>");
    if (region != null) {
        out.println("  is " + region + ".<br/>");
    } else {
        out.println("  is not valid.<br/>");
    }
    out.println("  </p>");
    out.println("</card>");
    out.println("</wml>");
}

From source file:MyServlet.java

public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException {
    ServletOutputStream out = res.getOutputStream();
    res.setContentType("text/html");
    out.println("<html><head><title>Basic Form Processor Output</title></head>");
    out.println("<body>");
    out.println("<h1>Here is your Form Data</h1>");
    //extract the form data here
    String title = req.getParameter("title");
    String name = req.getParameter("name");
    String city = req.getParameter("city");
    String country = req.getParameter("country");
    String tel = req.getParameter("tel");
    String age = req.getParameter("age");
    // extracting data from the checkbox field
    String[] interests = req.getParameterValues("interests");
    //output the data into a web page
    out.println("Your title is " + title);
    out.println("<br>Your name is " + name);
    out.println("<br>Your city is " + city);
    out.println("<br>Your country is " + country);
    out.println("<br>Your tel is " + tel);
    out.println("<br>Your interests include<ul> ");
    for (int i = 0; i < interests.length; i++) {
        out.println("<li>" + interests[i]);
    }//from   ww w. j  a  v  a2 s .c  o m
    out.println("</ul>");
    out.println("<br>Your age is " + age);
    out.println("</body></html>");
}

From source file:controllers.ColorController.java

@RequestMapping("/getXls")
public void getXls(Map<String, Object> model, HttpServletResponse response) throws Exception {
    response.setContentType("application/octet-stream");
    response.setHeader("Content-Disposition", "attachment; filename=Color.xls");
    colorService.getXls().write(response.getOutputStream());
}

From source file:org.tdmx.server.sx.Servlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");

    PrintWriter writer = response.getWriter();
    writer.println("<html>");
    writer.println("<head><title>Hello World Servlet</title></head>");
    writer.println("<body>");
    writer.println("Hello World! How are you doing? secure=" + request.isSecure());
    // writer.println(" AccountId=" + accountService.getActivePartitionId());

    writer.println("</body>");
    writer.println("</html>");
    writer.close();/*from  w  ww. j  av  a2 s  .  c o m*/

}

From source file:org.terasoluna.gfw.functionaltest.app.download.ImageFileDownloadView.java

@Override
protected void addResponseHeader(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) {
    response.setHeader("Content-Disposition", "attachment; filename=Duke.png");
    response.setContentType("image/png");
}