File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Sending HTTP status for a failed command Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Servlets
Reply Bookmark "Sending HTTP status for a failed command" Watch "Sending HTTP status for a failed command" New topic
Author

Sending HTTP status for a failed command

Pete Nelson
Ranch Hand

Joined: Aug 30, 2010
Posts: 144

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?


OCPJP

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

Joined: Jan 10, 2002
Posts: 50693

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

For general errors, 500 is most appropriate.


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
 
 
subject: Sending HTTP status for a failed command
 
Threads others viewed
parse xml response in Jersey (RESTful webservice)
Tomcat Error 404
Problem sending Apple Push Notification using Java and REST
GUI client talk to servlet
REST vs SOAP - stateful/stateless
developer file tools

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture