Author
Edit response body and set a response code
Adriano Bellavita
Ranch Hand
Joined: Mar 11, 2010
Posts: 31
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.
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
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!
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
Adriano Bellavita
Ranch Hand
Joined: Mar 11, 2010
Posts: 31
Yes, I want set the body!
Lester Burnham
Rancher
Joined: Oct 14, 2008
Posts: 1337
posted Sep 17, 2010 03:23:23
0
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.
Ed Ward
Ranch Hand
Joined: Jan 30, 2006
Posts: 147
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?
David Newton
Author
Rancher
Joined: Sep 29, 2008
Posts: 12617
posted Sep 17, 2010 08:55:14
0
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.
subject: Edit response body and set a response code