• Post Reply Bookmark Topic Watch Topic
  • New Topic

Edit response body and set a response code

 
Adriano Bellavita
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
Adriano Bellavita
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, I want set the body!
 
Lester Burnham
Rancher
Posts: 1337
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 147
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!