This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

sendRedirect() method

 
Arun Maalik
Ranch Hand
Posts: 216
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all i am not getting the thing that has been wriiten in Head Fiirst Servlet Jsp page 137 that the sendRedirect() method through an IllegalStateException if you try to invoke it after "the response has allraedy been comited" Here what is mean of response comited kindly write a bit of code...
 
Kumar Garlapati
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PrintWriter pw = response.getWriter();
pw.write("it is commited"); // This is commited
response.sendRedirect("URLString"); // you can't do this, gets run time exception, saying all ready commited.

OR

response.sendRedirect("URLString 1");
response.sendRedirect("URLString 2");//you can't do this
[ February 04, 2008: Message edited by: Kumar Garlapati ]
 
Bosun Bello
Ranch Hand
Posts: 1510
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What they are saying is if after sending a response to the client, you then call sendredirect the exception will be thrown. Think about it, you can't ask the browser to make another request after you have already responded to the same request. I am sure the experts here can add more. However, in the meantime you can google or search the forum for sendRedirect.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you start writing information to the client, it first gets 'buffered', which means the information is held in memory so that large chunks of data can be sent rather than lots of small chunks. This is more efficient.

It also means that you can start building a response and then change your mind.

Now then.

There is an important thing about sendRedirect, where the information is sent in the HTTP header, which is the first bit of data sent to the client. Therefore the buffer needs to be cleared before a sendRedirect is sent to the client, otherwise it will not work.

But.

The buffer is finite in size, 8kb by default. If you put too much information in, the buffer will start sending data to the client. At this stage, it is too late to clear the buffer, and the response is considered 'committed', that is you are committed to sending the original response and cannot now send another response, such as a sendRedirect.

Hopefully this clears things up. The response gets buffered, the buffer gets committed (sent), and the sendRedirect cannot clear the buffer and fails.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Kumar Garlapati:


Not if the data is still buffered.
 
Kumar Garlapati
Ranch Hand
Posts: 38
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you, nice explaination.
 
Brij Garg
Ranch Hand
Posts: 234
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any method dat can decide how much buffer has been filled?
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My first reponse would be "have you looked?" but really that is just a round-about way of saying "I don't know"

I'll check the classes, but I doubt it. In practice it is better to choose your response and gather all of your data before going anywhere near the output stream or writer

OK: JspWriter does, ServletOutputStream does not.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic