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

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from w w  w .  ja  va 2s .  co  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");

    ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

    EmployeeJDBCTemplate employeeJDBCTemplate = (EmployeeJDBCTemplate) context.getBean("employeeJDBCTemplate");

    System.out.println("Searching Records...");

    Employee employee = employeeJDBCTemplate.getEmployee(Integer.parseInt(request.getParameter("employee_id")));

    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */

        out.println("Name - " + employee.getName());
        out.println("<br>Email - " + employee.getEmail());
        out.println("<br>Mobile - " + employee.getMobile());
        out.println("<br>Department - " + employee.getDepartment());
        out.println("<br>Salary - " + employee.getSalary());

    }
}

From source file:com.redoute.datamap.servlet.picture.DeletePicture.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from  w  w w  . ja  v a  2  s  . com*/
 *
 * @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");
    PrintWriter out = response.getWriter();
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);

    try {
        String name = policy.sanitize(request.getParameter("id"));

        ApplicationContext appContext = WebApplicationContextUtils
                .getWebApplicationContext(this.getServletContext());
        IPictureService datamapService = appContext.getBean(IPictureService.class);
        IFactoryPicture factoryPicture = appContext.getBean(IFactoryPicture.class);

        Picture sqlLib = factoryPicture.create(Integer.valueOf(name), null, null, null, null);
        datamapService.deletePicture(sqlLib);
        response.sendRedirect("Datamap.jsp");
    } finally {
        out.close();
    }
}

From source file:org.remus.marketplace.servlets.RecentListing.java

@Override
public void handleRequest(HttpServletRequest arg0, HttpServletResponse arg1)
        throws ServletException, IOException {
    arg1.setContentType("text/xml");
    try {/*  w  ww .  ja  v  a  2  s .  com*/
        JAXBContext newInstance = JAXBContext.newInstance(Marketplace.class,
                org.remus.marketplace.xml.Market.class);

        Marketplace marketplace = new Marketplace();

        List<Node> findByCategoriesId = nodeDao
                .find(new AdvancedCriteria().setMaxResults(10).addOrder(Order.desc(Node.CHANGED)));

        Recent recent = new Recent();
        recent.setCount(findByCategoriesId.size());
        for (Node findById : findByCategoriesId) {
            org.remus.marketplace.xml.Node node = XMLBuilder.buildNode(serverPrefix, findById);
            recent.getNode().add(node);
        }
        marketplace.setRecent(recent);
        Marshaller createMarshaller = newInstance.createMarshaller();
        XMLSerializer xmlSerializer = XMLBuilder.getXMLSerializer(arg1.getOutputStream());
        createMarshaller.marshal(marketplace, xmlSerializer.asContentHandler());
    } catch (JAXBException e) {
        throw new ServletException(e);
    }

}

From source file:Servlet.AddAdminServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//w ww .  jav a2s. com
 *
 * @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");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet AddAdminServlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet AddAdminServlet at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

From source file:getDeals.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from  w  w  w  .j av a  2  s  .  com*/
 *
 * @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("application/json;charset=UTF-8");
    PrintWriter out = response.getWriter();
    try {
        String url = "https://tgl.standardchartered.com/bridgeheadi18n/V6/offers/offerdetails?country=SG&lang=en&deviceId=0&offerVersion=0&couponVersion=0&categoryVersion=0";
        HttpClient client = new DefaultHttpClient();
        HttpGet req = new HttpGet(url);
        HttpResponse res = client.execute(req);
        BufferedReader rd = new BufferedReader(new InputStreamReader(res.getEntity().getContent()));
        String line = "";
        String total = "";
        while ((line = rd.readLine()) != null) {
            total += line;
        }

        total = total.substring(0, total.length());
        out.println(total);
    } finally {
        out.close();
    }
}

From source file:com.tc.admin.UpdateCheckRequestTest.java

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

    String[] expectedParams = { "kitID", "pageID", "id", "os-name", "jvm-name", "jvm-version", "platform",
            "tc-version", "tc-product", "source" };
    Map paramMap = request.getParameterMap();
    for (String expectedParam : expectedParams) {
        if (paramMap.get(expectedParam) == null) {
            response.getWriter().println("Missing parameter '" + expectedParam + "'");
            return;
        }/*from   ww w  . j  av  a2s .c  om*/
    }
    if (paramMap.size() != expectedParams.length) {
        response.getWriter()
                .println("Parameter count doesn't match expected count='" + expectedParams.length + "'");
        return;
    }
    response.getWriter().println("OK");
}

From source file:de.tuttas.servlets.ClientConfig.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//www .  j av  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");
    try (PrintWriter out = response.getWriter()) {
        /* TODO output your page here. You may use following sample code. */
        Config c = Config.getInstance();
        Set set = c.clientConfig.keySet();

        Iterator<?> keys = set.iterator();

        while (keys.hasNext()) {

            String key = (String) keys.next();

            out.println("var " + key + "=\"" + c.clientConfig.get(key) + "\";");

        }
        out.println("var debug=" + c.debug + ";");
        out.println("var servername=\"" + request.getServerName() + "\";");
        out.println("var serverport=" + request.getServerPort() + ";");

    }
}

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

From source file:net.mindengine.oculus.frontend.web.controllers.report.ReportUploadFileController.java

@Override
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setContentType("text/html");
    PrintWriter pw = response.getWriter();
    log.info("uploading file");
    boolean isMultipart = ServletFileUpload.isMultipartContent(request);
    if (isMultipart) {

        DefaultMultipartHttpServletRequest multipartRequest = (DefaultMultipartHttpServletRequest) request;

        int i = 0;
        boolean bNext = true;
        while (bNext) {
            i++;//from  w  ww  .  j a  v a2s  .  com
            MultipartFile multipartFile = multipartRequest.getFile("file" + i);
            if (multipartFile != null) {
                fileId++;
                Date date = new Date();
                String path = FileUtils.generatePath(date);
                String fullDirPath = config.getDataFolder() + File.separator + path;
                File dir = new File(fullDirPath);
                dir.mkdirs();
                //FileUtils.mkdirs(config.getDataFolder() + File.separator + path);

                String fileType = FileUtils.getFileType(multipartFile.getOriginalFilename()).toLowerCase();
                path += File.separator + date.getTime() + "_" + fileId + "." + fileType;

                File file = new File(config.getDataFolder() + File.separator + path);
                file.createNewFile();

                FileOutputStream fos = new FileOutputStream(file);
                fos.write(multipartFile.getBytes());
                fos.close();

                if (fileType.equals("png") || fileType.equals("jpg") || fileType.equals("gif")
                        || fileType.equals("bmp")) {
                    pw.println("[uploaded]?type=image-" + fileType + "&object=" + date.getTime() + "&item="
                            + fileId);
                }
                if (fileType.equals("txt") || fileType.equals("json") || fileType.equals("xml")) {
                    pw.println("[uploaded]?type=text-" + fileType + "&object=" + date.getTime() + "&item="
                            + fileId);
                }
            } else
                bNext = false;
        }
    } else
        pw.print("[error]not a multipart content");
    return null;
}

From source file:org.ktunaxa.referral.server.mvc.InMemoryCmisDocumentView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    Document doc = cmisService.getById((String) model.get(DOCUMENT_ID));
    response.setContentType(doc.getContentStreamMimeType());
    response.setContentLength((int) doc.getContentStreamLength());
    if (SAVE.equals((String) model.get(TYPE))) {
        response.setHeader("Content-Disposition", "attachment; filename=\"" + doc.getName() + "\"");
    }//  ww w .  j  a v  a  2 s .c  o m
    IOUtils.copy(doc.getContentStream().getStream(), response.getOutputStream());
}