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.jamespot.glifpix.wrappers.SampleHtmlTestService.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    response.setStatus(HttpServletResponse.SC_OK);

    PrintWriter out = response.getWriter();

    String htmlPage = FileUtils.readFileToString(new File(props.getProperty("tagsServer.testPage")));

    out.println(htmlPage);//w  w w.  jav  a  2s .c o  m
}

From source file:com.seer.datacruncher.profiler.spring.ProfilerTreeController.java

public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    try {//from ww  w  .ja v  a2 s. co m

        response.setContentType("application/json");
        PrintWriter out = response.getWriter();
        ObjectMapper mapper = new ObjectMapper();
        TableMetaDataBL tmBL = new TableMetaDataBL();
        String json = mapper.writeValueAsString(tmBL.loadTree(request));
        out.println(json);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

From source file:SessionSnoop.java

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

    HttpSession session = req.getSession();

    Integer count = (Integer) session.getAttribute("count");
    if (count == null)
        count = new Integer(1);
    else/*from   w ww .j a v a  2s .co  m*/
        count = new Integer(count.intValue() + 1);
    session.setAttribute("count", count);

    out.println("<HTML><HEAD><TITLE>Session Count</TITLE></HEAD>");
    out.println("<BODY><H1>Session Count</H1>");

    out.println("You've visited this page " + count + ((count == 1) ? " time." : " times."));

    out.println("<P>");

    out.println("<H3>Here is your saved session data:</H3>");
    Enumeration e = session.getAttributeNames();
    while (e.hasMoreElements()) {
        String name = (String) e.nextElement();
        out.println(name + ": " + session.getAttribute(name) + "<BR>");
    }

    out.println("<H3>Here are some vital stats on your session:</H3>");
    out.println("Session id: " + session.getId() + " <I>(keep it secret)</I><BR>");
    out.println("New session: " + session.isNew() + "<BR>");
    out.println("Timeout: " + session.getMaxInactiveInterval());
    out.println("<I>(" + session.getMaxInactiveInterval() / 60 + " minutes)</I><BR>");
    out.println("Creation time: " + session.getCreationTime());
    out.println("<I>(" + new Date(session.getCreationTime()) + ")</I><BR>");
    out.println("Last access time: " + session.getLastAccessedTime());
    out.println("<I>(" + new Date(session.getLastAccessedTime()) + ")</I><BR>");

    out.println("Requested session ID from cookie: " + req.isRequestedSessionIdFromCookie() + "<BR>");
    out.println("Requested session ID from URL: " + req.isRequestedSessionIdFromURL() + "<BR>");
    out.println("Requested session ID valid: " + req.isRequestedSessionIdValid() + "<BR>");

    out.println("<H3>Test URL Rewriting</H3>");
    out.println("Click <A HREF=\"" + res.encodeURL(req.getRequestURI()) + "\">here</A>");
    out.println("to test that session tracking works via URL");
    out.println("rewriting even when cookies aren't supported.");

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

From source file:GetCookiesServlet.java

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

    Cookie[] cookies = request.getCookies();

    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    pw.println("<B>");
    for (int i = 0; i < cookies.length; i++) {
        String name = cookies[i].getName();
        String value = cookies[i].getValue();
        pw.println("name = " + name + "; value = " + value);
    }//from   w  w w  .ja v a2 s.co m
    pw.close();
}

From source file:com.thoughtworks.go.server.web.FileView.java

private void seContentType(boolean needToZip, HttpServletResponse response, String filename) {
    response.setContentType(getMimeType(filename, needToZip));
}

From source file:de.onyxbits.raccoon.net.ResourceHandler.java

@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    response.setContentType("application/octet-stream");
    response.setStatus(HttpServletResponse.SC_OK);
    if (target.toLowerCase().endsWith("png")) {
        response.setContentType("image/png");
    }/*  ww w .  j a  v a 2  s . c  o  m*/
    if (target.toLowerCase().endsWith("css")) {
        response.setContentType("text/css");
    }

    // Prevent path traversal -> all resources must be in /web, no sub
    // directories.
    String tmp = target.substring(target.lastIndexOf('/'), target.length());

    InputStream in = getClass().getResourceAsStream("/web" + tmp);
    OutputStream out = response.getOutputStream();

    IOUtils.copy(in, out);
    in.close();
    out.flush();
    out.close();

    baseRequest.setHandled(true);
}

From source file:feedme.controller.LogoutServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*  w  ww.j ava 2 s.  c o m*/
 *
 * @param request servlet request
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    response.setCharacterEncoding("UTF-8");

}

From source file:com.yuga.ygplatform.modules.sys.web.AreaController.java

@ResponseBody
@RequestMapping(value = "treeData")
public List<Map<String, Object>> treeData(@RequestParam(required = false) Long extId,
        HttpServletResponse response) {
    response.setContentType("application/json; charset=UTF-8");
    List<Map<String, Object>> mapList = Lists.newArrayList();
    // User user = UserUtils.getUser();
    List<Area> list = areaService.findAll();
    for (int i = 0; i < list.size(); i++) {
        Area e = list.get(i);//from  w ww  .  ja va 2  s  .  c o m
        if ((extId == null)
                || (!extId.equals(e.getId()) && (e.getParentIds().indexOf("," + extId + ",") == -1))) {
            Map<String, Object> map = Maps.newHashMap();
            map.put("id", e.getId());
            // map.put("pId",
            // !user.isAdmin()&&e.getId().equals(user.getArea().getId())?0:e.getParent()!=null?e.getParent().getId():0);
            map.put("pId", e.getParent() != null ? e.getParent().getId() : 0);
            map.put("name", e.getName());
            mapList.add(map);
        }
    }
    return mapList;
}

From source file:controllers.CCOController.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=CARCOMPOPTION.xls");
    carCompletionOptionService.getXls().write(response.getOutputStream());
}

From source file:org.openmrs.module.usagestatistics.web.view.csv.AbstractCSVView.java

/**
 * @see org.springframework.web.servlet.view.AbstractView
 *///from  w w w . j  av a2 s . c  o m
@Override
@SuppressWarnings("unchecked")
protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    // Respond as a CSV file
    response.setContentType("text/csv");
    response.setHeader("Content-Disposition", "attachment; filename=\"" + getFilename(model) + "\"");

    // Disable caching
    response.setHeader("Pragma", "No-cache");
    response.setDateHeader("Expires", 0);
    response.setHeader("Cache-Control", "no-cache");

    // Write data
    writeValues(model, response.getWriter());
}