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.conwet.silbops.connectors.comet.SubCometConnectionTest.java

@Test
@SuppressWarnings("unchecked")
public void shouldSendMessage() throws IOException, EndPointException {

    HttpServletResponse respose = mock(HttpServletResponse.class);
    when(resource.getResponse()).thenReturn(respose);
    when(respose.getWriter()).thenReturn(writer);

    final JSONObject payload = new JSONObject();
    payload.put("key", "value");
    Message message = new Message() {

        @Override/*from   w  ww  .j  a v a  2s .  com*/
        public JSONObject getPayloadAsJSON() {

            return payload;
        }
    };

    instance.sendMessage(message);

    ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
    verify(writer).println(captor.capture());
    verify(writer).flush();

    assertThat(captor.getValue()).contains("SilboPS.recvMessage").contains(endpointID)
            .contains(payload.toJSONString().replaceAll("\"", "\\\\\""));
}

From source file:net.sourceforge.jwebunit.tests.util.HeadersServlet.java

protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.write(HtmlHelper.getStart("Received headers"));
    out.write("<h1>Received headers</h1>\n<p>Headers are:<br/>");
    /*/* w w  w.  ja  v a2  s  .c  om*/
     * Prints headers as name=[value] separated
     * by <BR/>
     */
    java.util.Enumeration headers = request.getHeaderNames();
    for (; headers.hasMoreElements();) {
        String h = headers.nextElement().toString();
        String v = request.getHeader(h);
        out.write(h + "=[" + v);
        if (headers.hasMoreElements()) {
            out.write("]<br/>\n");
        }
    }
    out.write("]</p>\n");
    String ref = request.getHeader("Referer");
    if (ref == null) {
        if (request.getParameterValues("myReferer") != null) {
            ref = request.getParameterValues("myReferer")[0];
        }
    }
    out.write(HtmlHelper.getLinkParagraph("return", ref));
    out.write(HtmlHelper.getEnd());
}

From source file:com.github.vatbub.vatbubgitreports.Main.java

public void doGet(HttpServletRequest request, HttpServletResponse response) {
    try {/*from w w  w .j  av  a2 s.  c  o  m*/
        // Print a basic page
        response.getWriter()
                .println("<html>\n" + "<body>\n" + "<h2>Hello World!</h2>\n" + "</body>\n" + "</html>");
    } catch (IOException e) {
        e.printStackTrace();
        response.setStatus(500);
    }
}

From source file:ru.mystamps.web.controller.RobotsTxtController.java

@GetMapping(Url.ROBOTS_TXT)
@SuppressWarnings("PMD.AvoidDuplicateLiterals")
public void getRobotsText(HttpServletResponse response) {
    response.setContentType("text/plain");
    response.setCharacterEncoding("UTF-8");

    try {// ww w.ja v a  2  s .  co  m
        PrintWriter writer = response.getWriter();

        writer.println("# robots.txt for " + Url.PUBLIC_URL);
        writer.println("User-Agent: *");
        writer.println("Disallow: " + Url.REGISTRATION_PAGE);
        writer.println("Disallow: " + Url.ACTIVATE_ACCOUNT_PAGE);
        writer.println("Disallow: " + Url.AUTHENTICATION_PAGE);
        writer.println("Disallow: " + Url.LOGIN_PAGE);
        writer.println("Disallow: " + Url.ADD_COUNTRY_PAGE);
        writer.println("Disallow: " + Url.ADD_SERIES_PAGE);
        writer.println("Disallow: " + Url.ADD_CATEGORY_PAGE);
        writer.println("Disallow: " + Url.FORBIDDEN_PAGE);
        writer.println("Disallow: " + Url.NOT_FOUND_PAGE);
        writer.println("Disallow: " + Url.INTERNAL_ERROR_PAGE);
        writer.println("Sitemap: " + Url.PUBLIC_URL + Url.SITEMAP_XML);
    } catch (IOException ex) {
        LOG.error("Can't return robots.txt: {}", ex.getMessage());
    }
}

From source file:org.dspace.app.webui.cris.controller.RSSStatController.java

public ModelAndView daily(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
    arg1.setContentType("application/rss+xml");
    processRSSStat(arg0, StatSubscription.FREQUENCY_DAILY).output(arg1.getWriter());
    arg1.getWriter().flush();//from   w  ww.  j a v a  2 s . com
    return null;
}

From source file:org.mitre.openid.connect.view.UserInfoView.java

protected void writeOut(JsonObject json, Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) {
    try {/*www.  ja v  a  2 s.c  om*/
        Writer out = response.getWriter();
        gson.toJson(json, out);
    } catch (IOException e) {

        logger.error("IOException in UserInfoView.java: ", e);

    }

}

From source file:MyServlet.java

public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    try {// w w w .ja  v  a 2 s .c  o m
        res.setContentType("text/plain; charset=UTF-8");
        PrintWriter out = res.getWriter();

        String charset = req.getParameter("charset");

        String text = req.getParameter("text");

        BufferedReader reader = new BufferedReader(
                new InputStreamReader(new StringBufferInputStream(text), charset));
        text = reader.readLine();

        out.println("Received charset: " + charset);
        out.println("Received text: " + text);
        out.println("Received text (escaped): " + toUnicodeEscapeString(text));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:com.Accenture.NewServlet.java

/**
 * Handles the HTTP <code>GET</code> method.
 *
 * @param request servlet request/*from   ww w  . ja v a 2s  . co m*/
 * @param response servlet response
 * @throws ServletException if a servlet-specific error occurs
 * @throws IOException if an I/O error occurs
 */
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    try (PrintWriter out = response.getWriter()) {
        ApplicationContext r = new ClassPathXmlApplicationContext("../../WEB-INF/applicationContext.xml");

        learnerDao dao = (learnerDao) r.getBean("d");
        out.println("<body id=\"load\" onload=\"infor();\">");
        out.println("<div id=\"show\">");
        out.println("<table border=1>");
        out.println(
                "<tr><td>Name</td><td>Address</td><td>Email</td><td>ID</td><td>PhoneNumber</td><td>Gender</td><td>Pin</td></tr>");
        List<learnerspojo> listemp = dao.getLearners();

        for (learnerspojo cn : listemp) {

            out.println("<tr><td>" + cn.getLID() + "</td>");
            out.println("<td>" + cn.getLName() + "</td>");

        }
        out.println("</table>");
        out.println("</div>");

        out.println("<div id=\"infor\">");

        out.println("</div>");

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

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

}

From source file:org.craftercms.core.util.spring.mvc.GsonView.java

@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    model = filterModel(model);/*from   w  w w .ja  v  a2 s.c  o m*/

    Writer output = response.getWriter();

    if (prefixJson) {
        output.write(JSON_ANTI_HIJACKING_PREFIX);
    }

    if (renderSingleAttributeAsRootObject && model.size() == 1) {
        gson.toJson(model.values().iterator().next(), output);
    } else {
        gson.toJson(model, output);
    }
}

From source file:org.dspace.app.webui.cris.controller.RSSStatController.java

public ModelAndView weekly(HttpServletRequest arg0, HttpServletResponse arg1) throws Exception {
    arg1.setContentType("application/rss+xml");
    processRSSStat(arg0, StatSubscription.FREQUENCY_WEEKLY).output(arg1.getWriter());
    arg1.getWriter().flush();// ww  w  . j av  a2 s .c  o m
    return null;
}