Advice: Redesign since this will be a support nightmare for you in the
future since the design is not in the spirit of how the spec works.

Now onto the real solution (over simplified .. but google can expand
on each sub-idea)

Create a ServletResponseWrapper and pass that to the forward() method
where the ServletResponseWrapper does 2 things
1) Extends all instances of header manipulation (setHeader(...))
2) Extends the output writing capabilities and pushes everything to a buffer

HttpServletResponse wrappedResponse = new
MyHttpServletResponseWrapper(response);
resource.forward(request, wrappedResponse);

// Add code to loop over all headers set during forward() and set them
on response now
response.getWriter().append("prepend string");
response.getWriter().append(wrappedResponse.yourBuffer());
response.getWriter().append("postpend string"); // newly invented word

-Tim


On Tue, Sep 29, 2020 at 5:31 AM Nicolò Boschi <boschi1...@gmail.com> wrote:
>
> Hi all,
> I would like to know how to append (or prepend) some content in a Servlet,
> after RequestDispatcher#forward is called.
>
> @Override
>     public void doGet(HttpServletRequest request, HttpServletResponse
> response)
>             throws ServletException, IOException {
>
>         final String finalUri = ... // compute some resource URI;
>         RequestDispatcher resource = request.getRequestDispatcher(finalUri);
>
>         response.getWriter().append("prepend string");
>         resource.forward(request, response);
>     }

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
For additional commands, e-mail: users-h...@tomcat.apache.org

Reply via email to