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:ch.ralscha.extdirectspring.controller.SSEWriter.java

public SSEWriter(HttpServletResponse response) {
    this.response = response;

    response.setContentType(EVENT_STREAM.toString());
    response.setCharacterEncoding(EVENT_STREAM.getCharSet().name());
}

From source file:com.dreamwork.web.FrontJsonController.java

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html; charset=UTF-8");
    ServletOutputStream out = response.getOutputStream();

    String message = "Not supported";

    out.write(message.getBytes("UTF-8"));
    out.flush();//ww w . j  a  v a  2s . co m

}

From source file:getPlayersRanks.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*  ww w  .ja  v a 2s.  c om*/
 *
 * @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");
    JSONArray json = new JSONArray();
    DBHandler db = new DBHandler();
    ArrayList<Player> players = db.getPlayers();
    for (int i = 0; i < players.size(); i++) {
        JSONObject JSONplayer = new JSONObject();
        Player player = players.get(i);
        JSONplayer.put("name", player.getName());
        JSONplayer.put("score", player.getScore());
        json.add(JSONplayer);

    }
    response.getWriter().write(json.toString());

}

From source file:net.sourceforge.jwebunit.tests.util.HeadersServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.write(HtmlHelper.getStart("Received headers"));
    out.write("<h1>Received headers</h1>\n<p>Headers are:<br/>");
    /*//www.  j  a  v  a2s .  c  om
     * Prints headers as name=[value] separated
     * by <BR/>
     */
    java.util.Enumeration headers = request.getHeaderNames();
    for (; headers.hasMoreElements();) {
        String h = headers.nextElement().toString();
        String v = request.getHeader(h);
        out.write(h + "=[" + v);
        if (headers.hasMoreElements()) {
            out.write("]<br/>\n");
        }
    }
    out.write("]</p>\n");
    String ref = request.getHeader("Referer");
    if (ref == null) {
        if (request.getParameterValues("myReferer") != null) {
            ref = request.getParameterValues("myReferer")[0];
        }
    }
    out.write(HtmlHelper.getLinkParagraph("return", ref));
    out.write(HtmlHelper.getEnd());
}

From source file:comsat.sample.servlet.SampleServletApplication.java

@SuppressWarnings("serial")
@Bean//from  www .ja  v  a  2  s  . c  o m
public Servlet dispatcherServlet() {
    return new FiberHttpServlet() {
        @Override
        @Suspendable
        public void doGet(HttpServletRequest req, HttpServletResponse res)
                throws ServletException, IOException {
            try {
                Fiber.sleep(10);
                res.setContentType("text/plain");
                res.getWriter().append("Hello World");
            } catch (InterruptedException | SuspendExecution ex) {
                throw new AssertionError(ex);
            }
        }
    };
}

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

From source file:org.mitre.uma.view.ResourceSetEntityView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) {

    response.setContentType("application/json");

    HttpStatus code = (HttpStatus) model.get("code");
    if (code == null) {
        code = HttpStatus.OK; // default to 200
    }/*w w  w . jav a2s . c  om*/

    response.setStatus(code.value());

    String location = (String) model.get("location");
    if (!Strings.isNullOrEmpty(location)) {
        response.setHeader(HttpHeaders.LOCATION, location);
    }

    try {

        Writer out = response.getWriter();
        ResourceSet rs = (ResourceSet) model.get("entity");

        JsonObject o = new JsonObject();

        o.addProperty("_id", rs.getId().toString()); // send the id as a string
        o.addProperty("user_access_policy_uri", config.getIssuer() + "manage/resource/" + rs.getId());
        o.addProperty("name", rs.getName());
        o.addProperty("uri", rs.getUri());
        o.addProperty("type", rs.getType());
        o.add("scopes", JsonUtils.getAsArray(rs.getScopes()));
        o.addProperty("icon_uri", rs.getIconUri());

        gson.toJson(o, out);

    } catch (IOException e) {

        logger.error("IOException in ResourceSetEntityView.java: ", e);

    }
}

From source file:servlets.Tickets.java

/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request//from w w  w.  ja  va  2s .com
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("application/json");
    Map<String, String[]> paramMap = request.getParameterMap();

    if (paramMap.isEmpty()) {
        try (PrintWriter out = response.getWriter()) {
            SelectTicketListTemplate ticketList = new SelectTicketListTemplate(response.getStatus(),
                    DataHelper.getInstance().getTicketIds());
            ObjectMapper mapper = new ObjectMapper();
            mapper.writeValue(out, ticketList);
        } catch (Exception e) {

        }
        //?   ID  ? Issue
    }
}

From source file:com.adeptj.runtime.servlet.CryptoServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) {
    String text = req.getParameter("text");
    if (StringUtils.isEmpty(text)) {
        resp.setContentType("text/plain");
        ResponseUtil.write(resp, "request parameter [text] can't be null!!");
    } else {/* www. j  a  v  a 2 s . com*/
        resp.setContentType("application/json");
        String salt = CryptoSupport.saltBase64();
        ResponseUtil.write(resp, String.format(RESP_JSON_FORMAT, salt, CryptoSupport.hashBase64(text, salt)));
    }
}

From source file:com.demandware.vulnapp.challenge.impl.MD5Challenge.java

public Object handleChallengeRequest(DIVAServletRequestWrapper req) {
    String download = req.getParameter(DOWNLOAD_PARAM);
    if (!StringUtils.isBlank(download)) {
        HttpServletResponse response = req.getResponse();
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + this.b64WarLocation.getName());

        try (FileInputStream fileIn = new FileInputStream(this.b64WarLocation);
                ServletOutputStream out = response.getOutputStream()) {

            IOUtils.copy(fileIn, out);//  www . j a v  a  2 s  .c  o  m

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}