Append content to OutputStream after RequestDispatcher#forward

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Append content to OutputStream after RequestDispatcher#forward

Nicolò Boschi
Hi all,
I would like to know how to append (or prepend) some content in a Servlet,
after RequestDispatcher#forward is called.

Example code:

class MyServlet extends HttpServlet {


@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);
    }

}



I know Javadoc says
'Uncommitted output in the response buffer is automatically cleared before
the forward.'
so obviously my code does not work properly.
I also tried using RequestDispatcher#include but I need to keep response
headers, added during the forward/inclusion.

So:
1) Is there any way to append content after/before forward?
2) Is there any way to keep response headers added during resource
inclusion?
3) Other ideas..?

Ty in advance

Nicolò Boschi
Reply | Threaded
Open this post in threaded view
|

Re: Append content to OutputStream after RequestDispatcher#forward

funkman
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 <[hidden email]> 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: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Append content to OutputStream after RequestDispatcher#forward

Christopher Schultz-2
In reply to this post by Nicolò Boschi
Nicolò,

On 9/29/20 05:31, Nicolò Boschi wrote:

> I would like to know how to append (or prepend) some content in a Servlet,
> after RequestDispatcher#forward is called.
>
> Example code:
>
> class MyServlet extends HttpServlet {
>
>
> @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);
>     }
>
> }

If you want to add content before/after the target, why not use
include() instead of forward()?

-chris

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Append content to OutputStream after RequestDispatcher#forward

funkman
Forward needed to be used due to this in the original email

> I also tried using RequestDispatcher#include but I need to keep response
> headers, added during the forward

And include() is not allowed to set headers.

-Tim

On Tue, Sep 29, 2020 at 2:27 PM Christopher Schultz
<[hidden email]> wrote:
>
> If you want to add content before/after the target, why not use
> include() instead of forward()?
>
> -chris

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: Append content to OutputStream after RequestDispatcher#forward

Nicolò Boschi
Yes I also need to keep headers derived from included/forwarded servlet

Thank you Tim, I already thought your solution and it should work, but it
is a bit tricky
I was wondering if there is a more elegant solution

I'm not strictly bound to forward method if there is a better solution

Il Mar 29 Set 2020, 20:34 Tim Funk <[hidden email]> ha scritto:

> Forward needed to be used due to this in the original email
>
> > I also tried using RequestDispatcher#include but I need to keep response
> > headers, added during the forward
>
> And include() is not allowed to set headers.
>
> -Tim
>
> On Tue, Sep 29, 2020 at 2:27 PM Christopher Schultz
> <[hidden email]> wrote:
> >
> > If you want to add content before/after the target, why not use
> > include() instead of forward()?
> >
> > -chris
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [hidden email]
> For additional commands, e-mail: [hidden email]
>
> Nicolò Boschi