Running and rendering JSP inside a filter

I have a JSP that contains content at the top of my page, let him call it header.jsp. I would like to make this JSP and wash it to the user before creating the rest of my page for performance reasons. (See here for an explanation of performance.)

The best way I can do this is to create a filter called FlushingFilter and write it to the JSP in response, and then wash it before doing the rest of the chain. As a proof of concept, I manually converted header.jsp into 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 () setting gives the desired behavior, and the page runs pretty fast.

But before starting, I would like to make it cleaner, if possible, by using a JSP program call inside the filter instead of working with println () manual calls. The closest solution to this I found is the first answer to this question , but it involves calling the include () method on RequestDispatcher. As far as I know, I do not have access to any RequestDispatcher inside my filter, although this may just be due to the inexperience of my JSP / servlet.

Does anyone know how I can programmatically call JSP this way and return its output in String format?

+3
source share
1 answer

, RequestDispatcher , JSP/,

.

request.getRequestDispatcher("/WEB-INF/header.jsp").include(request, response);
response.flushBuffer();
+6

All Articles