In this tutorial, we will use Spring 3 to render a JSP and capture the output to a String.  Why might you want to do this?  Specifically this need comes up in two instances:

  1. You want to use parameterized JSP pages to send emails to end-users.  You may want to do this in a separate Thread that doesn’t have access to the original user request. Yes, you could use Freemarker or Velocity to build a dynamic email system.  But you have an investment in JSP technology, and would like to use it to produce emails on the fly.
  2. You want to conditionally include a JSP, but you want to render it in the background for speed reasons.  Yes, you could use AJAX for this, but for some reason you can’t or don’t want to.

The goals of this tutorial are:

  • To render an arbitrary JSP, and return the rendered page as a String.
  • To render the JSP without access to any end-user HttpServletRequest, as it might not be available when we need it.
  • To be able to render any normal JSP.  This includes: using normal taglibs in the JSP such as JSTL and Spring tags, being able to render a full HTML page or just a fragment JSP, and being able to include other JSP pages inside our JSP if we need to (that is, use jsp:include), and the use of message bundles (that is, spring:message).
  • The JSP that we render should be Locale-aware, for both messages and numeric formats.

Full source code for this tutorial is available on GitHub.

Continue to the tutorial….

Read More