aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes problem with response commitment Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "problem with response commitment" Watch "problem with response commitment" New topic
Author

problem with response commitment

Eliot Skywalker
Greenhorn

Joined: Oct 22, 2003
Posts: 18
It is always emphisized that sending output when the response iscommitted will cause a java.lang.IllegalStateException to be raised
but how to identify a response is committed?
in other words, in which conditions is the response committed?
I am kind of confused with that.

thanks
Eliot


Every man dies, not every man really lives.
Sathya Sankar
Ranch Hand

Joined: Sep 16, 2000
Posts: 67
Hi Eliot,
It is always emphisized that sending output when the response iscommitted will cause a java.lang.IllegalStateException to be raised

Actually, sending output after the response is committed is perfectly alright. But what should not be done once the response is committed is trying to send a response all over again - examples are requestDispatcher.forward, response.sendError, response.sendRedirect, response.resetBuffer and response.reset.
but how to identify a response is committed?

You could use response.isCommitted() in your servlet code to determine if the response has been committed or not. Suggest you check using this call before invoking any operation that could throw the IllegalStateException.
in other words, in which conditions is the response committed?

Response gets committed when the buffer is filled or when there is no buffer at all. You can access/manipulate the buffer using methods provided in the javax.servlet.ServletResponse interface.
Ciao,
GSS


SCJP, SCJD, SCWCD 1.3, SCWCD 1.4, SCBCD
blue sky
Greenhorn

Joined: Jul 20, 2003
Posts: 12
Hi
Could you please give an example that it throws must be IllegalStateException.
Although buffer is filled response doesn't commited yet
Ex : I have code:
..........
out.print("<html><body>test"); // buffer is filled
getRequestDispatcher().forward(req,resp);
out.print(".....");
..........
but it works fine and ofcourse "test" isn't printed to the output, it means that buffer is filled response doesn't commited yet.
Mohit
Greenhorn

Joined: Oct 31, 2003
Posts: 6
Here you go ........

package test;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class Servlet1 extends HttpServlet {
static final private String CONTENT_TYPE = "text/html";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>Servlet1</title></head>");
out.println("<body>");
out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("</body></html>");
out.flush();//explicitly comminting the response
try{
response.sendRedirect("Jsp1.jsp");

}catch(Exception e){
System.out.print(e.toString());
}
}
//Clean up resources
public void destroy() {
}
}
Sathya Sankar
Ranch Hand

Joined: Sep 16, 2000
Posts: 67
Hi Trang,
out.print("<html><body>test"); // buffer is filled
getRequestDispatcher().forward(req,resp);
out.print(".....");

What is wrong with your example is that the buffer is not filled yet. When the buffer size is not set explicitly, servlet containers use a default value (typically 1KB). Since the text you have written is only a few bytes, the buffer does not get filled and consequently the response too stays uncommitted.
mohaces has given a detailed example.
Ciao,
GSS
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem with response commitment