2.8. Direct Rendering

Pages support a direct rendering mode where you can render directly to the servlet response and bypass the page template rendering. This is useful for scenarios where you want to render non HTML content to the response, such as a PDF or Excel document. To do this:

A direct rendering example is provided below.

/**
 * Render the Java source file as "text/plain".
 *
 * @see Page#onGet()
 */
public void onGet() {
    String filename = ..

    HttpServletResponse response = getContext().getResponse();

    response.setContentType("text/plain");
    response.setHeader("Pragma", "no-cache");

    ServletContext context = getContext().getServletContext();

    InputStream inputStream = null;
    try {
        inputStream = context.getResourceAsStream(filename);

        PrintWriter writer = response.getWriter();

        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        String line = reader.readLine();

        while (line != null) {
            writer.println(line);
            line = reader.readLine();
        }

        setPath(null);

    } catch (IOException ioe) {
        ioe.printStackTrace();

    } finally {
        ClickUtils.close(inputStream);
    }
}