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

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws ServletException, java.io.IOException {
    resp.setContentType("text/html");
    java.io.PrintWriter out = resp.getWriter();
    resp.setHeader("Expires", "Tues, 01 Jan 1980 00:00:00 GMT");
    String uri = req.getRequestURI();

    HttpSession session = req.getSession();
    String user = (String) session.getAttribute(USER_KEY);

    if (user == null) {
        login(out, uri);/*  ww  w .jav a2  s .  c  o  m*/
        return;
    }
    out.println("<html>");
    out.println("<head>");
    out.println("<title>Welcome</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<center><h2>Welcome to our site!</h2>");
    out.println("</center><br><br>");
    out.println("</body>");
    out.println("</html>");
    out.flush();
}

From source file:com.redoute.datamap.servlet.datamap.UpdateDatamap.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//w  ww. jav  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"));
        String columnName = policy.sanitize(request.getParameter("columnName"));
        String value = policy.sanitize(request.getParameter("value"));

        ApplicationContext appContext = WebApplicationContextUtils
                .getWebApplicationContext(this.getServletContext());
        IDatamapService datamapService = appContext.getBean(IDatamapService.class);
        datamapService.updateDatamap(name, columnName, value);

        out.print(value);
    } finally {
        out.close();
    }
}

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

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//  ww  w . j  a  va 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");
    PrintWriter out = response.getWriter();
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);

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

        ApplicationContext appContext = WebApplicationContextUtils
                .getWebApplicationContext(this.getServletContext());
        IPictureService datamapService = appContext.getBean(IPictureService.class);
        datamapService.updatePicture(name, columnName, value);

        out.print(value);
    } finally {
        out.close();
    }
}

From source file:com.pm.myshop.controller.FilesController.java

@RequestMapping("/pictures/{thumb}/{fileName}")
@ResponseBody/*w w w .  ja v a  2s  .co  m*/
public void viewFiles(@PathVariable("fileName") String fileName, @PathVariable("thumb") String thumb,
        HttpServletRequest request, HttpServletResponse response) throws IOException {
    try {

        String path = request.getRealPath("/");

        response.setContentType("image/jpeg");

        InputStream inputStream = new FileInputStream(path + "../../files/" + thumb + "/" + fileName + ".jpg");
        IOUtils.copy(inputStream, response.getOutputStream());
        response.flushBuffer();

    } catch (IOException ex) {
        System.out.println(ex.getMessage());
        response.getWriter().print("File Not Found");
    }

}

From source file:delete.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*from w  w w .j  a  v a2  s .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");

    Socket s = new Socket("localhost", 9999);

    DataInputStream in = new DataInputStream(s.getInputStream());
    DataOutputStream outD = new DataOutputStream(s.getOutputStream());

    outD.writeUTF(request.getParameter("employee_id"));

    in.readUTF();

    s.close();

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

        out.println("Deleted");
    }
}

From source file:TestServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods./*  w w  w.  j  a va2 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");
    PrintWriter out = response.getWriter();
    try {
        /* 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 TestServlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet TestServlet at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    } finally {
        out.close();
    }
}

From source file:bookUtilities.BookListServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  w ww  . ja v  a  2 s.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");
    JSONArray books = getBooks();
    PrintWriter printout = response.getWriter();
    printout.print(books);
    printout.flush();
}

From source file:com.zlfun.framework.excel.ExcelUtils.java

public static <T> void httpOutput(String name, Class<T> clazz, List<T> list, HttpServletRequest request,
        HttpServletResponse response) {
    try {//from w  w  w  .  j av a  2s .  c  om
        request.setCharacterEncoding("UTF-8");//request???
        String fileName = name;//??
        String contentType = "application/vnd.ms-excel";//?
        String recommendedName = new String(fileName.getBytes(), "iso_8859_1");//????
        response.setContentType(contentType);//?
        response.setHeader("Content-Disposition", "attachment; filename=" + recommendedName + "\"");//
        response.resetBuffer();
        //?
        ServletOutputStream sos = response.getOutputStream();
        write(name, clazz, list, sos);
        sos.flush();
        sos.close();
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(ExcelUtils.class.getName()).log(Level.SEVERE, null, ex);
    } catch (IOException ex) {
        Logger.getLogger(ExcelUtils.class.getName()).log(Level.SEVERE, null, ex);
    }
}

From source file:control.ProcesoVertimientosServlets.SeleccionarInformacionGeneral.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request//from   w  w  w  . j a  va2 s.c  om
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("charset=UTF-8");
    //Obtenemos la opcion
    int opcion = Integer.parseInt(request.getParameter("opcion"));

    switch (opcion) {

    //Obtenemos todas las actividades economicas
    case 1: {
        getInformacionesGenerales(request, response);
    }
        break;

    //Obtenemos una actividad economica especifica.    
    case 2: {
        getInformacionGeneral(request, response);
    }
        break;
    }
}

From source file:Employee.deleteCustomer.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from  w w  w . j  ava2  s .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");
    try {
        String ssn = request.getParameter("ssn");
        Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");

        Connection con = DriverManager.getConnection("jdbc:sqlserver://localhost;user=sa;password=nopw");

        Statement st = con.createStatement();

        String query = "UPDATE [MatchesFromAbove].[dbo].[Customer] " + "SET Active = 0 " + "WHERE SSN = '" + ssn
                + "'";
        st.executeUpdate(query);

        //loop through result set and create the json objects

    } catch (Exception e) {
        System.out.println(e.getMessage());
        return;
    }
}