Example usage for javax.servlet.http HttpServletResponse getWriter

List of usage examples for javax.servlet.http HttpServletResponse getWriter

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse getWriter.

Prototype

public PrintWriter getWriter() throws IOException;

Source Link

Document

Returns a PrintWriter object that can send character text to the client.

Usage

From source file:com.bounswe2015group5.xplore.AllCommentsServlet.java

/**
 * Returns all comments of a contribution as an array of JSONObjects.
 * Each object includes content, and date of the comment with 
 * its creator's name and surname.//from w ww  .  j  a  v  a  2 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
 * @author Can Guler
 */
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        String contribId = request.getParameter("contribId");
        out.print(getAllComments(contribId).toString());
    }
}

From source file:MainServer.UploadServlet.java

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    DiskFileItemFactory factory = new DiskFileItemFactory();
    String upload = this.getServletContext().getRealPath("/upload/");
    String temp = System.getProperty("java.io.tmpdir");
    factory.setSizeThreshold(1024 * 1024 * 5);
    factory.setRepository(new File(temp));
    ServletFileUpload servleFileUplaod = new ServletFileUpload(factory);
    try {//w ww .jav  a2  s  .c  o  m
        List<FileItem> list = servleFileUplaod.parseRequest(request);
        for (FileItem item : list) {
            String name = item.getFieldName();
            InputStream is = item.getInputStream();
            if (name.contains("content")) {
                System.out.println(inputStream2String(is));
            } else {
                if (name.contains("file")) {
                    try {
                        inputStream2File(is, upload + "\\" + item.getName());
                    } catch (Exception e) {
                    }
                }
            }
        }
        out.write("success");
    } catch (FileUploadException | IOException e) {
        out.write("failure");
    }
    out.flush();
    out.close();
}

From source file:com.arifultonu.mappingServlet.LoginServlet.java

/**
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
 * methods.//from w w w .j a v  a 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");
    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 LoginServlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet LoginServlet at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

From source file:cs472.w1d5.servlet.Welcome.java

/**
 * Handles the HTTP <code>POST</code> method.
 *
 * @param request servlet request/* ww w. j a  v a2s  .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("text/html;charset=UTF-8");
    try (PrintWriter out = response.getWriter()) {
        out.println("<!DOCTYPE html>");
        out.println("<html>");
        out.println("<head>");
        out.println("<title>Servlet AddUser</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>We only process GET requests</h1>");
        out.println("</body>");
        out.println("</html>");
    }
}

From source file:me.uni.sushilkumar.geodine.util.YummlyImage.java

/** 
 * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
 * @param request servlet request//from  www  . j  av a 2 s. co m
 * @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 {
        String id = request.getParameter("id");
        JSONParser parser = new JSONParser();
        URL u = new URL("http://api.yummly.com/v1/api/recipe/" + id
                + "?_app_id=bad04ef2&_app_key=f2df74726bb514d9390abf1e6c9ad4f0");
        Object json = parser.parse(new BufferedReader(new InputStreamReader(u.openStream())));
        JSONObject obj = (JSONObject) json;
        JSONArray images = (JSONArray) obj.get("images");
        Iterator<JSONObject> it = images.iterator();
        JSONObject temp = it.next();
        String url = (String) temp.get("hostedLargeUrl");
        out.println("<h1>" + url + "</h1>");
    } catch (ParseException ex) {
        Logger.getLogger(YummlyImage.class.getName()).log(Level.SEVERE, null, ex);
    } finally {
        out.close();
    }
}

From source file:com.dm.platform.servlet.IndexServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse response)
        throws ServletException, IOException {

    response.setCharacterEncoding("utf-8");
    response.setContentType("text/html;charset=utf-8");
    try (PrintWriter out = response.getWriter()) {
        out.println("<!DOCTYPE HTML>");
        out.println("<HTML>");
        out.println("      <HEAD>");
        out.println("    <TITLE>A Servlet</TITLE>");
        out.println("    <meta http-equiv=\"content-type\" " + "content=\"text/html; charset=utf-8\">");
        out.println(" </HEAD>");
        out.println("       <BODY>");
        out.println("             Hello AnnotationServlet.");
        out.println("     </BODY>");
        out.println("</HTML>");
        out.flush();//w  w  w . j a v  a2 s .c o m
    }

}

From source file:de.topicmapslab.majortom.server.http.util.JSONView.java

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

    response.setCharacterEncoding("UTF-8");

    if (callback != null) {
        response.setContentType("application/javascript");
        response.getWriter().write(callback);
        response.getWriter().write("(");
        response.getWriter().write(jsonString);
        response.getWriter().write(");");
    } else {//from w  w  w.  j a v  a  2s  . com
        response.setContentType("application/json");
        response.getWriter().write(jsonString);
    }
}

From source file:de.undercouch.gradle.tasks.download.SslTest.java

@Override
protected Handler[] makeHandlers() throws IOException {
    ContextHandler sslHandler = new ContextHandler("/" + SSL) {
        @Override//  w w w. j av  a 2s . c  o  m
        public void handle(String target, HttpServletRequest request, HttpServletResponse response,
                int dispatch) throws IOException, ServletException {
            response.setStatus(200);
            PrintWriter rw = response.getWriter();
            rw.write("Hello");
            rw.close();
        }
    };
    return new Handler[] { sslHandler };
}

From source file:MailAccessor.java

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

    response.setContentType("text/html");
    java.io.PrintWriter out = response.getWriter();
    out.println("<html><head><title>Email Reader</title></head><body>");

    handleMessages(request, out);/*from w w w .j  ava  2s .  co m*/

    out.println("</body></html>");
    out.close();
}

From source file:controller.LoadImageServlet.java

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 LoadImageServlet</title>");
        out.println("</head>");
        out.println("<body>");
        out.println("<h1>Servlet LoadImageServlet at " + request.getContextPath() + "</h1>");
        out.println("</body>");
        out.println("</html>");
    }/*from   ww w  .  j a v  a 2s. co  m*/
}