• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

sendError() and setStatus()

 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kyle.
May i know where from you get all these informations ?

regards,
Sam
 
Mathew Sam
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Kyle,
In Tomcat both
setStatus(600) and sendError(600)
triggers error-page. Can I ignore this ?

regards,
Sam
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
600 is not a defined satus code. I tried with that number randomly.
regards,
Sam
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic