This week's book giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds and have James Denton on-line!
See this thread for details.
Win a copy of Learning OpenStack Networking: Build a solid foundation in virtual networking technologies for OpenStack-based clouds this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Edit response body and set a response code  RSS feed

 
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.
 
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!
 
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.
 
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?
 
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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!