aspose file tools
The moose likes Servlets and the fly likes Edit response body and set a response code Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Edit response body and set a response code" Watch "Edit response body and set a response code" New topic
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
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

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Edit response body and set a response code
 
Similar Threads
AJAX : How can I set response type other than 200 ("OK") in Server Response
Is it possible to set the response status code in Apache using JAVA
execute servlet without returning a page
A simple servlet question
Servlet - Urgent help required.