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:MyServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {

    res.setContentType("text/html");
    PrintWriter out = res.getWriter();

    String name = req.getParameter("name");
    out.println("<HTML>");
    out.println("<HEAD><TITLE>Hello, " + name + "</TITLE></HEAD>");
    out.println("<BODY>");
    out.println("Hello, " + name);
    out.println("</BODY></HTML>");
}

From source file:fr.penet.servlet.StartCrawlServlet.java

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/plain");
    String seed = req.getParameter("seed");
    if (StringUtils.isEmpty(seed)) {
        resp.getWriter().println("Error : empty seed parameter");
        return;//from ww  w  .  j  a  v a  2  s . c  om
    }
    String threads = req.getParameter("threads");
    int threadInt = 5;
    if (StringUtils.isNumeric(threads)) {
        threadInt = Integer.parseInt(threads);
    }
    CustomCrawler crawler = new CustomCrawler();
    int runId = crawler.startCollectUrls(seed, threadInt);
    resp.getWriter().println("Crawler run with id " + runId + " and " + threadInt + " threads started");
}

From source file:fr.penet.servlet.StopCrawlServlet.java

@Override
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    resp.setContentType("text/plain");
    String runIdString = req.getParameter("id");
    if (StringUtils.isEmpty(runIdString)) {
        resp.getWriter().println("Error : empty id parameter");
        return;/*from   w  w  w  .  j a  va 2 s  .  c  om*/
    }
    if (!StringUtils.isNumeric(runIdString)) {
        resp.getWriter().println("Error : id parameter is not an integer");
        return;
    }
    int runId = Integer.parseInt(runIdString);
    CustomCrawler crawler = new CustomCrawler();
    crawler.endCollectUrls(runId);
    resp.getWriter().println("Requested stop of crawl run " + runId);
}

From source file:com.tianjunwei.download.DownLoadController.java

@RequestMapping(value = { "/download" }, method = { RequestMethod.GET })
public void download(HttpServletResponse response) throws IOException {
    response.setContentType("application/octet-stream");
    response.addHeader("Content-Disposition", "attachment; filename=download.txt");
    OutputStream outputStream = response.getOutputStream();
    outputStream.write("sdsdfs".getBytes());
    outputStream.close();/* w  w w.  ja v  a2s .c  om*/
}

From source file:MainServlets.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/plain");
    PrintWriter out = res.getWriter();
    count++;//from  www  . ja v a 2  s .c  om
    out.println("Since loading (and with a possible initialization");
    out.println("parameter figured in), this servlet has been accessed");
    out.println(count + " times.");
}

From source file:MyServlet.java

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

    int siteIndex = Math.abs(random.nextInt()) % sites.size();
    String site = (String) sites.elementAt(siteIndex);

    res.setStatus(res.SC_MOVED_TEMPORARILY);
    res.setHeader("Location", site);
}

From source file:SiteSelector.java

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

    int siteIndex = Math.abs(random.nextInt()) % sites.size();
    String site = sites.elementAt(siteIndex);

    res.setStatus(res.SC_MOVED_TEMPORARILY);
    res.setHeader("Location", site);
}

From source file:UnknownEncodingServlet.java

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

    response.setContentType("text/html; charset=ISO-8850-1");
    PrintWriter out = response.getWriter();
}

From source file:AuthenticationSnoop.java

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

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

    out.println("<H1>This is a password protected resource</H1>");
    out.println("<PRE>");
    out.println("User Name: " + req.getRemoteUser());
    String name = (req.getUserPrincipal() == null) ? null : req.getUserPrincipal().getName();
    out.println("Principal Name: " + name);
    out.println("Authentication Type: " + req.getAuthType());
    out.println("Is a Manager: " + req.isUserInRole("manager"));
    out.println("</PRE>");
    out.println("</BODY></HTML>");
}

From source file:MyServlet.java

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

    out.println("Request Headers:");
    out.println();//from   w  w  w  . j  a va 2s .  co  m
    Enumeration names = req.getHeaderNames();
    while (names.hasMoreElements()) {
        String name = (String) names.nextElement();
        Enumeration values = req.getHeaders(name); // support multiple values
        if (values != null) {
            while (values.hasMoreElements()) {
                String value = (String) values.nextElement();
                out.println(name + ": " + value);
            }
        }
    }
}