• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

setStatus() vs sendError()

 
raj malhotra
Ranch Hand
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 134
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 288
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic