This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes setStatus() vs sendError() Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Reply Bookmark "setStatus() vs sendError()" Watch "setStatus() vs sendError()" New topic
Author

setStatus() vs sendError()

raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
HI
I have written the following line and i am getting error on web page properly.
response.sendError(response.SC_HTTP_VERSION_NOT_SUPPORTED);


but when i write the following line i am getting wep page without any error.
response.setStatus(response.SC_HTTP_VERSION_NOT_SUPPORTED);


I am interested to know then what is the practical use of setStatus? i mean how we will use it on client side.
Tarun Yadav
Ranch Hand

Joined: Sep 20, 2007
Posts: 134
Take a look at the documentation for sendError() and setStatus()

Basically, setStatus() is used to set non-error codes while sendError() is used for 4xx and 5xx set of statuses.

Also, sendError() will set the error handling/ forwarding mechanism into motion ( the error pages defined etc ) while setStatus() will not.

By the way, your code uses an instance of the HttpServletResponse to get the contants for the status codes. This is against convention. You should use the class name to access static members ( variables and methods ); like so:
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
HI Tarun
Thanks for reply i got the difference between both methods but i want to know how can i use setStatus() functionality in my code? I am setting status but where is the use?
[ October 23, 2007: Message edited by: raj malhotra ]
Tarun Yadav
Ranch Hand

Joined: Sep 20, 2007
Posts: 134
You usually won't be using this, it'll be handled by the container.

For example, when you use a sendRedirect(), it causes a status code of 302 : temporary redirect ( though temporary redirect could also be 303 or 307, so I'm not sure ) as the response.
raj malhotra
Ranch Hand

Joined: Feb 22, 2007
Posts: 285
Thanks for your reply Tarun
so what i understand from your reply is that we dont have to use setStatus() in our code .Its used by the web container developers to develope the container .so its used inside container to do jobs.

and regarding sendRedirect() its 301 given on page 134 of HFSJ
[ October 24, 2007: Message edited by: raj malhotra ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setStatus() vs sendError()
 
Similar Threads
setStatus()
Does setStatus on response object trigger the error page mechanism ?
regarding setStatus()
sendError() and setStatus()
problem setting custom header in response