[Logo]
Forums Register Login
Edit response body and set a response code
Hi to all,

I'm in trouble today.

I'm working with the servlet in Java: I want to edit the response body and set a status different from 200.

So this is my code:


So, if I cut the first line - response.setStatus(404); - from the code, response code is 200 (obviously...) and response body is correct.

But if I insert the setStatus method, response code is 404 (yet obvious) but response body is empty...

So my questions are:

- can I write response body if response status is different from 200?
- in which way?

TY in advance,

Adriano.
I think, for the custom message, you can use the overloaded version of the method. Have a look on this API.

In that,

The container clears the buffer and sets the Location header, preserving cookies and other headers.

So we can't set our custom body? Please confirm!
Yes, I want set the body!
 

can I write response body if response status is different from 200?


Most other status codes are for errors - it doesn't make sense for them to have bodies.
But if you did want some sort of custom error message you might make use of an error page configuration in web.xml or some other descriptor, mapped to the codes you want to catch and a resource to forward to. Like maybe a custom JSP error page or error servlet.
No?
 

Lester Burnham wrote:

can I write response body if response status is different from 200?


Most other status codes are for errors - it doesn't make sense for them to have bodies.


That's not necessarily true; I've used various status codes with JSON bodies (but it could be anything) for Ajax response handling.

This thread has been viewed 3772 times.

All times above are in ranch (not your local) time.
The current ranch time is
Oct 16, 2018 22:49:53.