private Object getPageObject(String name) {
return getJspContext().getAttribute(name, PageContext.PAGE_SCOPE);
}
public void doTag() throws JspException, IOException {
final JspContext jspContext = getJspContext();
final Class<?> resolvingClass = (Class<?>) jspContext.getAttribute(RequestDispatcherWrapper.RESOLVING_CLASS_ATTRIBUTE_NAME,
PageContext.REQUEST_SCOPE);
final String basePath = (String) jspContext.getAttribute(RequestDispatcherWrapper.BASE_PATH_ATTRIBUTE_NAME,
PageContext.REQUEST_SCOPE);
final ServletConfig servletConfig = (ServletConfig) getPageObject(PageContext.CONFIG);
final ServletContext servletContext = servletConfig.getServletContext();
for (Class<?> clazz = resolvingClass; clazz != Object.class; clazz = clazz.getSuperclass()) {
final String template = basePath + TemplateHelper.getAbsolutePath(clazz, page, '/');
if (servletContext.getResource(template) != null) {
// Tomcat returns a RequestDispatcher even if the JSP file doesn't exist so check if the resource exists first.
final RequestDispatcher dispatcher = servletContext.getRequestDispatcher(template);
if (dispatcher != null) {
try {
final HttpServletRequest request = (HttpServletRequest) getPageObject(PageContext.REQUEST);
final HttpServletResponse response = (HttpServletResponse) getPageObject(PageContext.RESPONSE);
dispatcher.include(request,
new Wrapper(response, new PrintWriter(jspContext.getOut())));
} catch (ServletException e) {
throw new JspException(e);
}
return;
}