• Post Reply Bookmark Topic Watch Topic
  • New Topic

Purpose of setStatus() in HttpServletResponse object

 
Suresh Khanna
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,
In realtime when do you want to use setStatus() in your programs.
For displaying error pages we have sendError(). Explanation appreciated
Thanks
Suresh
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that I answered this with Marty Hall's 'Core Servlets and JavaServer Pages' in front of me
setStatus sets an arbitrary status code. sendError will send a 404 (Not Found) and sendRedirect will send a 302. They are specific versions of the setStatus method.
Marty also says to be sure to call setStatus before sending any other data (you can also attach your own information page rahter than relying on the default browser page for that status code)
Dave
 
Suresh Khanna
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You Dave,
For your answer. I am having Java Servlet programming by Jason Hunter from Orielly guys. I know what the method will do. I asked the purpose for setting the arbitraty status code in your applications.
Regards
Suresh K
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at a servlet as code that results in a HTTP page being returned, it is hard to see the point if returning your own status codes since the servlet container takes care of it all for you.
If, however, you are building a more intelligent servlet, such as a front controller that manages some type of proprietary authentication, you'll want to be able to return 403 and 409 status codes. This is a bit of a contrived example, but it is conceivable that you may want to allow the servlet container to manage some HTTP responsibilities and take over some others programatically. Using setStatus makes this possible, otherwise you just wouldn't be able to.
Dave
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!