GeeCON Prague 2014*
The moose likes Servlets and the fly likes sendRedirect() method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "sendRedirect() method" Watch "sendRedirect() method" New topic
Author

sendRedirect() method

Arun Maalik
Ranch Hand

Joined: Oct 25, 2005
Posts: 216
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

Joined: Feb 04, 2008
Posts: 38
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 ]

Regards,<br />Kumar
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
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.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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

Joined: Mar 06, 2001
Posts: 13459

Originally posted by Kumar Garlapati:


Not if the data is still buffered.
Kumar Garlapati
Ranch Hand

Joined: Feb 04, 2008
Posts: 38
thank you, nice explaination.
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Is there any method dat can decide how much buffer has been filled?
David O'Meara
Rancher

Joined: Mar 06, 2001
Posts: 13459

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.
 
GeeCON Prague 2014
 
subject: sendRedirect() method