File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Sending HTTP status for a failed command Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Sending HTTP status for a failed command" Watch "Sending HTTP status for a failed command" New topic

Sending HTTP status for a failed command

Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 147

I have a REST application that uses a single controller servlet to parse path-based commands, and then return XML data based on the command. For example, I map my servlet to /MyApp/REST/*, and then /MyApp/REST/folder/12345 returns an XML representation of the folder with the id of 12345. /MyApp/REST/person/12345 returns an XML representation of the person who's ID is 12345.

When I initially wrote this app, I wrote it so if you used an invalid command (/MyApp/REST/folderrer/12345), it would return a help page showing what commands are available. That all worked fine. However, I am now trying to pull that data via AJAX, and I am seeing a problem where my app is returning a status of 200 regardless if the command is understood or not.

It looks like I need to use the HttpServletResponse.setStatus(int, String) method if I want to continue to write the response after setting the status. But my question is ... what status code should I use? Is it appropriate to use a 404 in this case? Or do I want to use something in the 5xx range? What have others done?

In preparing for battle I have always found that plans are useless, but planning is indispensable. -- Dwight D. Eisenhower
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63838

404 means not found. It is appropriate for any REST response where a resource is not found.

For general errors, 500 is most appropriate.

[Asking smart questions] [About Bear] [Books by Bear]
I agree. Here's the link:
subject: Sending HTTP status for a failed command
It's not a secret anymore!