Code example for PrintWriter

Methods: flush, write

0
 
    public void writeStock(AsyncContext actx, Stock stock) {
        HttpServletResponse response = (HttpServletResponse)actx.getResponse();
        try { 
            PrintWriter writer = response.getWriter();
            writer.write("STOCK#");//make client parsing easier
            writer.write(stock.getSymbol());
            writer.write("#");
            writer.write(stock.getValueAsString());
            writer.write("#");
            writer.write(stock.getLastChangeAsString());
            writer.write("#");
            writer.write(String.valueOf(stock.getCnt()));
            writer.write("\n");
            writer.flush();
            response.flushBuffer();
        }catch (IOException x) {
            try {actx.complete();}catch (Exception ignore){/* Ignore */}
        } 
    }