treefrog
treefrog

Reputation: 1057

how to check if getwriter or getoutputstream is already used when called inside a servlet file

I need to write append to the response a hidden tag after the server is done generating the jsp response. I keep getting the illegalstate exception. So I used getWriter instead of getOutputStream now its complaining of this:

java.lang.IllegalStateException: strict servlet API: cannot call getWriter() after getOutputStream()

I am overriding getWriter in a response wrapper which I pass down the chain in my filter

on return from that method I append stuff with write

Since I am getting errors on using either one of getWriter or getOutputStream, I assume I have to check somehow which of these has already been used.

Upvotes: 2

Views: 3185

Answers (2)

Stephen C
Stephen C

Reputation: 719149

Since I am getting errors on using either one of getWriter or getOutputStream, I assume I have to check somehow which of these has already been used.

There is no standard way to do that.

If you are going to implement this using a custom wrapper, then the wrapper itself needs to keep track of whether getWriter or getOutputStream has been called. You could expose this state by adding an extra public method to your wrapper implementation class.

Upvotes: 2

BillRobertson42
BillRobertson42

Reputation: 12883

You can modify the response headers or data after its been generated with a servlet filter. Its a little bit of work to implement one the first time, but once you've done it its not that bad and they're quite powerful.

Upvotes: 0

Related Questions