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:nl.tue.gale.tools.servlets.ExportFile.java

public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String filename = request.getParameter("fileName");
    String author = request.getParameter("author");
    boolean created = false;
    try {// w  ww . j  a  va  2s  . co  m
        String graphauthorfiles = (String) AHAStatic.AUTHORFILESPATH;
        String aharoot = (String) AHAStatic.config(getServletContext()).Get("AHAROOT");
        String fileUrl = aharoot + graphauthorfiles.substring(1) + author + "/" + filename + ".aha";
        created = copyToConfig(fileUrl, filename);
    } catch (Exception e) {
        e.printStackTrace();
    }
    out.println(created);
}

From source file:com.netease.channel.controller.BaseController.java

protected void successRes(HttpServletResponse respose) {
    respose.setContentType(JSON_CONTENT_TYPE);
    try {//from w ww . j  a  v  a2s  . co m
        respose.getWriter().write(SUC_RES);
        respose.getWriter().close();
    } catch (IOException e) {
        LOG.info("Controller response error");
    }
}

From source file:com.netease.channel.controller.BaseController.java

protected void errorRes(HttpServletResponse respose) {
    respose.setContentType(JSON_CONTENT_TYPE);
    try {// w w w .  j av a2 s.c om
        respose.getWriter().write(FAIL_RES);
        respose.getWriter().close();
    } catch (IOException e) {
        LOG.info("Controller response error");
    }
}

From source file:ems.web.controller.other.ResumeController.java

@RequestMapping(value = "/spellCheck", method = RequestMethod.POST)
@ResponseBody/*from   w  w w. jav a  2  s  . c  o m*/
public void speelCheck(String data, HttpServletResponse response) {
    response.setCharacterEncoding("UTF-8");
    try {
        response.getWriter().println(resumeServiceImpl.spellCheck(data));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

From source file:FlavorListServlet.java

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

    // Get the bounds of the ranks to be listed
    // or use defaults

    int lowLimit = getLimit(request.getParameter("lowLimit"), 0);
    int highLimit = getLimit(request.getParameter("highLimit"), 100);

    Connection con = null;/* www  .j av  a 2  s .  c om*/
    try {

        // Connect to the ice cream database

        Class.forName(JDBC_DRIVER);
        con = DriverManager.getConnection(URL);

        // Run a query to get the top flavors

        String sql = "SELECT  RANK, NAME" + "   FROM flavors" + "   WHERE RANK BETWEEN ? AND ?"
                + "   ORDER BY RANK";
        PreparedStatement pstmt = con.prepareStatement(sql);
        pstmt.setInt(1, lowLimit);
        pstmt.setInt(2, highLimit);
        ResultSet rs = pstmt.executeQuery();

        // Print as an ordered list

        out.println("<ol>");
        while (rs.next()) {
            int rank = rs.getInt(1);
            String name = rs.getString(2);
            out.println("   <li>" + name + "</li>");
        }
        out.println("</ol>");
    } catch (SQLException e) {
        throw new ServletException(e.getMessage());
    } catch (ClassNotFoundException e) {
        throw new ServletException(e.getMessage());
    }

    // Close the database

    finally {
        if (con != null) {
            try {
                con.close();
            } catch (SQLException ignore) {
            }
        }
    }
}

From source file:ShoppingCartViewerCookie.java

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

    String sessionid = null;//from   w w w .  ja v  a2 s .  c o m
    Cookie[] cookies = req.getCookies();
    if (cookies != null) {
        for (int i = 0; i < cookies.length; i++) {
            if (cookies[i].getName().equals("sessionid")) {
                sessionid = cookies[i].getValue();
                break;
            }
        }
    }

    // If the session ID wasn't sent, generate one.
    // Then be sure to send it to the client with the response.
    if (sessionid == null) {
        sessionid = generateSessionId();
        Cookie c = new Cookie("sessionid", sessionid);
        res.addCookie(c);
    }

    out.println("<HEAD><TITLE>Current Shopping Cart Items</TITLE></HEAD>");
    out.println("<BODY>");

    // Cart items are associated with the session ID
    String[] items = getItemsFromCart(sessionid);

    // Print the current cart items.
    out.println("You currently have the following items in your cart:<BR>");
    if (items == null) {
        out.println("<B>None</B>");
    } else {
        out.println("<UL>");
        for (int i = 0; i < items.length; i++) {
            out.println("<LI>" + items[i]);
        }
        out.println("</UL>");
    }

    // Ask if they want to add more items or check out.
    out.println("<FORM ACTION=\"/servlet/ShoppingCart\" METHOD=POST>");
    out.println("Would you like to<BR>");
    out.println("<INPUT TYPE=SUBMIT VALUE=\" Add More Items \">");
    out.println("<INPUT TYPE=SUBMIT VALUE=\" Check Out \">");
    out.println("</FORM>");

    // Offer a help page.
    out.println("For help, click <A HREF=\"/servlet/Help" + "?topic=ShoppingCartViewerCookie\">here</A>");

    out.println("</BODY></HTML>");
}

From source file:fi.helsinki.opintoni.security.AuthFailureHandler.java

@Override
public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response,
        AuthenticationException exception) throws IOException, ServletException {
    response.setStatus(HttpServletResponse.SC_UNAUTHORIZED);

    PrintWriter writer = response.getWriter();
    writer.write(exception.getMessage());
    writer.flush();/*from www.  j  ava  2s.  c o m*/
}

From source file:FormServlet.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>");

    RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/validate.js");

    dispatcher.include(request, response);

    out.println("<title>Help Page</title></head><body>");
    out.println("<h2>Please submit your information</h2>");

    out.println("<form action =\"" + request.getContextPath()
            + "/displayHeaders.jsp\" onSubmit=\" return validate(this)\">");

    out.println("<table border=\"0\"><tr><td valign=\"top\">");
    out.println("Your name: </td>  <td valign=\"top\">");
    out.println("<input type=\"text\" name=\"username\" size=\"20\">");
    out.println("</td></tr><tr><td valign=\"top\">");
    out.println("Your email: </td>  <td valign=\"top\">");
    out.println("<input type=\"text\" name=\"email\" size=\"20\">");
    out.println("</td></tr><tr><td valign=\"top\">");

    out.println("<input type=\"submit\" value=\"Submit Info\"></td></tr>");
    out.println("</table></form>");
    out.println("</body></html>");
    out.close();//  w ww.  ja  v  a  2 s .c  o  m
}

From source file:fi.helsinki.opintoni.security.CustomAuthenticationSuccessHandlerTest.java

private HttpServletResponse mockResponse() throws IOException {
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(mock(PrintWriter.class));
    return response;
}

From source file:org.mitre.secretsharing.server.FormSplitServlet.java

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    Writer w = new HtmlXSSWriter(resp.getWriter());

    try {/* w  w  w . j a v a 2s. co  m*/
        String secret = req.getParameter("secret");
        if (secret == null)
            throw new RuntimeException("No secret parameter");
        int totalParts;
        try {
            totalParts = Integer.parseInt(req.getParameter("total_parts"));
            if (totalParts < 1)
                throw new RuntimeException();
        } catch (Exception e) {
            throw new RuntimeException("Total parts not an integer at least 1.");
        }
        int requiredParts;
        try {
            requiredParts = Integer.parseInt(req.getParameter("required_parts"));
            if (requiredParts < 1 || requiredParts > totalParts)
                throw new RuntimeException();
        } catch (Exception e) {
            throw new RuntimeException(
                    "Required parts not an integer at least 1 and not more than total parts.");
        }
        boolean base64 = false;
        if (req.getParameter("base64") != null)
            base64 = Boolean.parseBoolean(req.getParameter("base64"));

        byte[] secretBytes;

        if (base64) {
            try {
                secretBytes = Base64Variants.MIME.decode(secret);
            } catch (Exception e) {
                throw new RuntimeException("Improper encoding of base64 secret");
            }
        } else
            secretBytes = secret.getBytes("UTF-8");

        Part[] parts = Secrets.splitPerByte(secretBytes, totalParts, requiredParts, rnd);

        for (Part part : parts) {
            w.write(part + "\n");
        }
    } catch (Throwable t) {
        if (t.getMessage() != null)
            w.write(t.getMessage());
        else
            w.write("error");
    }
}