tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alan Chaney <>
Subject Re: Committing sendRedirect()
Date Tue, 29 Jul 2008 09:54:26 GMT

Your code below is written as though the 'out' was like writing data to 
a console. The processing in a servlet is part of a  request/response 
cycle, where the 'request' tells it what to do and the response is the 

Because of the nature of the request/response cycle you only get one 
chance at the reply. The javadocs for HttpServletResponse.sendRedirect 
make this clear.

"If the response has already been committed, this method throws an 
IllegalStateException. After using this method the response should be 
considered to be committed and should not be written to."

In other words, in one servlet request/response cycle you get exactly 
one chance to issue a 'sendRedirect'. If you think this through you'll 
see that this is how it has to be:

Browser sends request to servlet
Servlet process request and sendsRedirect
Browser display response.

Your problem is that you are trying to generate a second response 
without a correspondng request. The first response is for the 'top 25' 
and the second is for 'all the rest', but by then you've already 
committed the response.

To achieve what I think you are trying to achieve would require you to 
create some mechanism where the lucene query results are processed in a 
separate thread and your application makes TWO requests - one for the 
first set of results and then one for the final set of results.

There are far too many different ways to do this to detail here - I hope 
this helps


Alan Chaney

Tore Eriksson wrote:
> Hi everybody,
> I have a problem with redirects in Tomcat 5.5.25. I am doing a Lucene
> search and would like to send a redirect after finding the top 25 hits,
> commit this response, and then continue processing the remaining hits.
> The relevant parts of the doPost() code are as below:
> final int PARTIAL_HITS = 25;
> for (int i = 0; i < hits.length(); i++) {
>   Document doc = hits.doc(i);
>   String citation = doc.get("citation");
>   /** Insert into table */
>   try {
>     insertId.setInt(1, java.lang.Integer.parseInt(citation));
>     insertId.executeUpdate();
>   }
>   catch (SQLException e) {
>     out.sendError(500, "Bad SQL insert: " + e);
>   }
>   catch (Exception e) {}
>   if (i == PARTIAL_HITS) {
>     // Intermittant redirect
>     out.sendRedirect(resultUrl);
>   }
> }
> insertId.close();
> if (!out.isCommitted()) {
>   // Final redirect
>   out.sendRedirect(resultUrl);
> }
> My problem is that the intermittant redirect is not committed until the
> function returns, which will take quite some time for some queries. I
> have tried HttpServletResponse.flushBuffer() and other possible
> variations. Any pointers would be most appreciated.
> Tore
> _______________________________________________________________
> Tore Eriksson [tore.eriksson ad]
> ---------------------------------------------------------------------
> To start a new topic, e-mail:
> To unsubscribe, e-mail:
> For additional commands, e-mail:
> !DSPAM:488e7ade203691909011899!

To start a new topic, e-mail:
To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message