Reputation: 33
I have a JSP that contains the content at the top of my page, let's call it header.jsp. I would like to render this JSP and flush it out to the user before building up the rest of my page, for performance reasons. (See here for an explanation of the performance benefit.)
The best way I can think to do this is to create a Filter called FlushingFilter, and have it write the contents of the JSP to the response and then flush it out before executing the rest of the chain. As a proof-of-concept, I manually converted header.jsp to a bunch of response.getWriter().println() calls inside my FlushingFilter, after which I call response.getWriter().flush() and then doFilter() to continue the filter chain. This println() setup yields the desired behavior, and the page is quite a bit faster.
But before launching, I'd like to make it cleaner if possible by programatically invoking the JSP inside of the filter instead of having to work with manual println() calls. The closest solution to this I've found is the first answer to this question, but it involves calling the include() method on RequestDispatcher. As far as I'm aware, I don't have access to any RequestDispatcher inside my filter, although that could just be my JSP/servlet inexperience talking.
Does anyone know how I can programatically invoke a JSP like this, and get back its output in String format?
Upvotes: 3
Views: 3355
Reputation: 1108872
I'm aware, I don't have access to any RequestDispatcher inside my filter, although that could just be my JSP/servlet inexperience talking
It's definitely available in the filter.
request.getRequestDispatcher("/WEB-INF/header.jsp").include(request, response);
response.flushBuffer();
Upvotes: 6