aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes sendError()  and setStatus() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "sendError()  and setStatus()" Watch "sendError()  and setStatus()" New topic
Author

sendError() and setStatus()

Mathew Sam
Ranch Hand

Joined: Dec 19, 2001
Posts: 124
1) what is the difference between sendError() method and setStatus() method of HttpServletResponse.
2) What is the diffrence of redirecting to a page using sendRedirect() or using RequestDispatcher ?

Can anybody clear these two things ?

regards,
Sam
Kyle Tang
Ranch Hand

Joined: Aug 22, 2002
Posts: 78
sendError will trigger the <error-page> defined in web.xml.
setStatus() won't. if you use setStatus, you have to provide the content to be returned. that is, you need out.print(...), etc, and set some HTTP response header if it is necessary. sendError will set all the headers automatically for you.
so setStatus() should only be used for setting normal HTTP response status, not for error status. for error status you should use sendError().
sendRedirect() and requestDispatcher.forward() is very very different.
1. sendRedirect set the "location" HTTP response header, and send back the HTTP response. your browser get the response, and then sends the new HTTP request to the URL set in "location" header.
RequestDispatcher is transparent to the browser, your web-container will go send the HTTP request to the URL, get the response, and send it back to the browser, the browser does not even know the resouse is actually from that URL.
2. sendRedirect(url), the url could be any URL, it could be out of your web-application, say, www.yahoo.com. but RequestDispatcher() can ONLY forward to the url that is part of your web-application, it could never be an absolute URL.


Kyle Tang<br />SCJP 91<br />SCWCD 96<br />SCBCD 95
Raj Paul
Ranch Hand

Joined: Jul 09, 2002
Posts: 77
Hi Kyle
Nice explanation, i know sendError() will commit the response, will setStatus also commits the response ?
Please clear this
Raj Paul
Mathew Sam
Ranch Hand

Joined: Dec 19, 2001
Posts: 124
Thanks Kyle.
May i know where from you get all these informations ?

regards,
Sam
Mathew Sam
Ranch Hand

Joined: Dec 19, 2001
Posts: 124
Kyle,
In Tomcat both
setStatus(600) and sendError(600)
triggers error-page. Can I ignore this ?

regards,
Sam
Vedhas Pitkar
Ranch Hand

Joined: Jan 27, 2001
Posts: 445
Good to have Kyle around ,isnt it?May I add that sendRedirect() happens on the client side while forward() happens on the server.Right.Kyle?
Kyle Tang
Ranch Hand

Joined: Aug 22, 2002
Posts: 78
sendError() will commit the response.
It will set the status code, set some headers if it is necessary, and set the content to the one defined in <error-page>, and send the HTTP response.
setStatus() will not commit the response. you need to do out.println(...) to write the contents, and then commit the response. nothing automatic, just as normal.
sendRedirect() happens on the client side, it is the client who go fetch the content of the new url, where forward() happens completely on server side, client won't see what happened behind.
Kyle Tang
Ranch Hand

Joined: Aug 22, 2002
Posts: 78
i am not sure what tomcat does for 600. what is 600? I don't see it in the HttpServletResponse API. when you call setStatus(600) without anything else, what do you see on your browser? (it might the error page provided by your browser)
you may try other error code, like 404, 500, to see the difference between setStatus and sendError.
I learn the stuff from "Servlet Programming", and "SCWCD exam study kit". The latter one is essential for the SCWCD exam.
Mathew Sam
Ranch Hand

Joined: Dec 19, 2001
Posts: 124
600 is not a defined satus code. I tried with that number randomly.
regards,
Sam
Maha Annadurai
Ranch Hand

Joined: Oct 27, 2002
Posts: 87
Along with above, another important difference between response.sendRedirect(..) and requestDispatcher.forward(..) is , with requestDispatcher, we preserve the current HttpServletRequest's attributes and parameters. Basically we use the SAME request object in both forwarding and forwarded resources. But, if you do sendRedirect we loose them all!
So if you had written your servlets in such a way that, the first forwarding servlet sets some request attribute and the forwarded jsp/servlet accessess them , your code will NOT work if you replace requestDispatcher with sendRedirect.
We have to be aware of this in practical servlet development.
Regards,
Maha Anna
[ November 30, 2002: Message edited by: Maha Annadurai ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sendError() and setStatus()
 
Similar Threads
Difference between sendError and setStatus methods...
setStatus() and sendError()
setStatus(),sendError() & IllegalStateException
Does setStatus on response object trigger the error page mechanism ?
setStatus Method of HttpServletResponse