It is really wierd. I create a
test servlet and deploy it to Tomcat 5.
Here is the code.
public class TestServlet extends HttpServlet {
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
//TODO Method stub generated by Lomboz
response.setContentType("text/html");
PrintWriter pw=response.getWriter();
pw.println("<b>Admin Login </b>");
pw.flush();
response.flushBuffer();
pw.println("response has been " + response.isCommitted());
pw.println(request.getContextPath() +"/servlet/HelloWorldExample");
response.sendRedirect(request.getContextPath() +"/servlet/HelloWorldExample");//BOOM! this is gonna explode
pw.close();
}
}
The return message I got is
Admin Login response has been
true /servlets-examples/servlet/HelloWorldExample
"true" means response is committed, then an IlleagalStateException must be thrown due to the following.
sendRedirect
public void sendRedirect(java.lang.String location)
throws java.io.IOException
Sends a temporary redirect response to the client using the specified redirect location URL. This method can accept relative URLs; the servlet container must convert the relative URL to an absolute URL before sending the response to the client. If the location is relative without a leading '/' the container interprets it as relative to the current request URI. If the location is relative with a leading '/' the container interprets it as relative to the servlet container root.
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.
Parameters:
location - the redirect location URL
Throws:
java.io.IOException - If an input or output exception occurs
IllegalStateException - If the response was committed or if a partial URL is given and cannot be converted into a valid URL