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:
get the servlet response object
set the content type on the response
get the response output stream
write to the output stream
close the output stream
set the page path to null to inform the ClickServlet that rendering has been completed
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); } }