PageContext pageContext = ServletActionContext.getPageContext();
/** * create a PageContext and render the template to PageContext.getOut() * * @see VelocityServlet#mergeTemplate(Template, Context, HttpServletResponse) for additional documentation */ protected void mergeTemplate(Template template, Context context, HttpServletResponse response) throws ResourceNotFoundException, ParseErrorException, MethodInvocationException, IOException, UnsupportedEncodingException, Exception { // save the old PageContext PageContext oldPageContext = ServletActionContext.getPageContext(); // create a new PageContext JspFactory jspFactory = JspFactory.getDefaultFactory(); HttpServletRequest request = (HttpServletRequest) context.get(ContextUtil.REQUEST); PageContext pageContext = jspFactory.getPageContext(this, request, response, null, true, 8192, true); // put the new PageContext into ActionContext ActionContext actionContext = ActionContext.getContext(); actionContext.put(ServletActionContext.PAGE_CONTEXT, pageContext); try { Writer writer = pageContext.getOut(); template.merge(context, writer); writer.flush(); } finally { // perform cleanup jspFactory.releasePageContext(pageContext); actionContext.put(ServletActionContext.PAGE_CONTEXT, oldPageContext); } }