Code example for PrintWriter

Methods: flush, println

0
        String mode = request.getParameter("out");
        PrintWriter pw = null;
 
        if (mode.equals("stream")) {
            OutputStreamWriter osw = new OutputStreamWriter(response.getOutputStream());
            pw = new PrintWriter(osw);
        } 
 
        if (pw == null) {
            pw = response.getWriter();
        } 
        response.setContentType("text/html");
        pw.println("<html><head><title>Servlet using " + mode + "</title></head>");
        pw.println("<body>This is a servlet using " + mode + " to output</body></html>");
        pw.flush();
    } 
}