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 code = null, message = null, type = null, uri = null;
    Object codeObj, messageObj, typeObj;
    Throwable throwable;/*w  ww . j av a2s . c o m*/

    // Retrieve the three possible error attributes, some may be null
    codeObj = req.getAttribute("javax.servlet.error.status_code");
    messageObj = req.getAttribute("javax.servlet.error.message");
    typeObj = req.getAttribute("javax.servlet.error.exception_type");
    throwable = (Throwable) req.getAttribute("javax.servlet.error.exception");
    uri = (String) req.getAttribute("javax.servlet.error.request_uri");

    if (uri == null) {
        uri = req.getRequestURI(); // in case there's no URI given
    }

    // Convert the attributes to string values
    if (codeObj != null)
        code = codeObj.toString();
    if (messageObj != null)
        message = messageObj.toString();
    if (typeObj != null)
        type = typeObj.toString();

    // The error reason is either the status code or exception type
    String reason = (code != null ? code : type);

    out.println("<HTML>");
    out.println("<HEAD><TITLE>" + reason + ": " + message + "</TITLE></HEAD>");
    out.println("<BODY>");
    out.println("<H1>" + reason + "</H1>");
    out.println("<H2>" + message + "</H2>");
    out.println("<PRE>");
    if (throwable != null) {
        throwable.printStackTrace(out);
    }
    out.println("</PRE>");
    out.println("<HR>");
    out.println("<I>Error accessing " + uri + "</I>");
    out.println("</BODY></HTML>");
}

From source file:com.mycompany.thymeleafspringapp.controller.DealsController.java

@RequestMapping(value = "/deals/{dealid}/{imgid}", method = RequestMethod.GET)
public void getImage(@PathVariable(value = "imgid") long imgId, @PathVariable(value = "dealid") long dealId,
        HttpServletResponse response) throws IOException {
    Users user = getUserDetails();/*from w w  w  .j a  v a  2s.  c  om*/
    response.setContentType("image/jpeg, image/jpg, image/png, image/gif");
    response.getOutputStream().write(ds.getScreenshotById(user.getUserId(), imgId));
    response.getOutputStream().close();
}

From source file:SettingandReadingCookies.java

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

    out.println("<HTML>");
    out.println("<HEAD>");
    out.println("<TITLE>");
    out.println("A Web Page");
    out.println("</TITLE>");
    out.println("</HEAD>");
    out.println("<BODY");

    Cookie[] cookies = request.getCookies();
    boolean foundCookie = false;

    for (int loopIndex = 0; loopIndex < cookies.length; loopIndex++) {
        Cookie cookie1 = cookies[loopIndex];
        if (cookie1.getName().equals("color")) {
            out.println("bgcolor = " + cookie1.getValue());
            foundCookie = true;//from   ww w. j  a  v a  2  s  .  c o m
        }
    }

    if (!foundCookie) {
        Cookie cookie1 = new Cookie("color", "cyan");
        cookie1.setMaxAge(24 * 60 * 60);
        response.addCookie(cookie1);
    }

    out.println(">");
    out.println("<H1>Setting and Reading Cookies</H1>");
    out.println("This page will set its background color using a cookie when reloaded.");
    out.println("</BODY>");
    out.println("</HTML>");
}

From source file:MyServlet.java

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

    out.println("<HTML>");
    out.println("<HEAD>");
    out.println("<TITLE>");
    out.println("A Web Page");
    out.println("</TITLE>");
    out.println("</HEAD>");
    out.println("<BODY");

    Cookie[] cookies = request.getCookies();
    boolean foundCookie = false;

    for (int i = 0; i < cookies.length; i++) {
        Cookie cookie1 = cookies[i];// ww w  .ja  va 2s.  c  o m
        if (cookie1.getName().equals("color")) {
            out.println("bgcolor = " + cookie1.getValue());
            foundCookie = true;
        }
    }

    if (!foundCookie) {
        Cookie cookie1 = new Cookie("color", "cyan");
        cookie1.setMaxAge(24 * 60 * 60);
        response.addCookie(cookie1);
    }

    out.println(">");
    out.println("<H1>Setting and Reading Cookies</H1>");
    out.println("This page will set its background color using a cookie when reloaded.");
    out.println("</BODY>");
    out.println("</HTML>");
}

From source file:com.ibm.ws.lars.rest.FrontPage.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    resp.setContentType(MediaType.APPLICATION_JSON);
    resp.setCharacterEncoding(StandardCharsets.UTF_8.name());
    PrintWriter printWriter = resp.getWriter();

    List<AssetFilter> filters = new ArrayList<>();
    filters.add(new AssetFilter("state",
            Arrays.asList(new Condition[] { new Condition(Operation.EQUALS, "published") })));
    int assetCount = serviceLayer.countAllAssets(filters, null);

    JsonGenerator frontPageJsonGenerator = new JsonFactory().createGenerator(printWriter);
    frontPageJsonGenerator.setPrettyPrinter(new DefaultPrettyPrinter());

    frontPageJsonGenerator.writeStartObject();
    frontPageJsonGenerator.writeStringField("serverName", "LARS");
    frontPageJsonGenerator.writeNumberField("assetCount", assetCount);
    frontPageJsonGenerator.writeEndObject();

    frontPageJsonGenerator.flush();/*from  w ww .  j a v  a2s .  c  o  m*/
    frontPageJsonGenerator.close();
}

From source file:com.laborun.servlet.UploadMyFile.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");
    //        System.out.println(request.getParameter("myfile"));
    if (ServletFileUpload.isMultipartContent(request)) {
        System.out.println("radwa");
        MultipartRequest m = new MultipartRequest(request, "E:\\ITI Work\\Projects\\LaboRunEtc\\New folder");

        Path path = Paths.get("E:\\ITI Work\\Projects\\LaboRunEtc\\sad.txt");
        byte[] array = Files.readAllBytes(path);
        System.out.println(array.length);
        AssignmentFiles assignment = new AssignmentFiles();
        assignment.setUploadFile(array);
        assignment.setUploadDate(new Date());

        AssignmentDaoInt upload = new AssignmentDaoImp();
        upload.uploadAssignment(assignment);

        //           response.sendRedirect("http://localhost:8082/trainee/buttons.jsp");
    }/*from   w ww.java  2s  .c o m*/
}

From source file:ErrorDisplay.java

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

    String code = null, message = null, type = null, uri = null;
    Object codeObj, messageObj, typeObj;
    Throwable throwable;//from  w w w .  ja  v a  2 s.com

    codeObj = req.getAttribute("javax.servlet.error.status_code");
    messageObj = req.getAttribute("javax.servlet.error.message");
    typeObj = req.getAttribute("javax.servlet.error.exception_type");
    throwable = (Throwable) req.getAttribute("javax.servlet.error.exception");
    uri = (String) req.getAttribute("javax.servlet.error.request_uri");

    if (uri == null) {
        uri = req.getRequestURI(); // in case there's no URI given
    }

    // Convert the attributes to string values
    if (codeObj != null)
        code = codeObj.toString();
    if (messageObj != null)
        message = messageObj.toString();
    if (typeObj != null)
        type = typeObj.toString();

    // The error reason is either the status code or exception type
    String reason = (code != null ? code : type);

    out.println("<HTML>");
    out.println("<HEAD><TITLE>" + reason + ": " + message + "</TITLE></HEAD>");
    out.println("<BODY>");
    out.println("<H1>" + reason + "</H1>");
    out.println("<H2>" + message + "</H2>");
    out.println("<PRE>");
    if (throwable != null) {
        throwable.printStackTrace(out);
    }
    out.println("</PRE>");
    out.println("<HR>");
    out.println("<I>Error accessing " + uri + "</I>");
    out.println("</BODY></HTML>");
}

From source file:authentication.DefaultAuthenticationFailureHandler.java

/** {@inheritDoc} */
@Override//from  w  w w  .j  a  va  2  s .co  m
public void onAuthenticationFailure(final HttpServletRequest request, final HttpServletResponse response,
        final AuthenticationException exception) throws IOException {
    response.setContentType(MediaType.APPLICATION_JSON_VALUE);
    response.setStatus(HttpStatus.UNAUTHORIZED.value());
    response.getOutputStream()
            .print(String.format(JSON, HttpStatus.UNAUTHORIZED.value(), exception.getMessage()));
}

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

From source file:edu.mayo.cts2.framework.plugin.service.lexevs.bulk.AbstractBulkDownloadController.java

protected void writeException(HttpServletResponse response, String message, int errorCode) {
    response.setContentType("text/plain; charset=utf-8");
    response.setStatus(errorCode);//from  w ww .  jav a2  s .  co m

    try {
        IOUtils.write(message, response.getOutputStream());
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}