You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by Qian Weichun <vi...@21cn.edu.cn> on 2000/10/26 05:31:41 UTC

I think that it is a bug.

The bug lies here:
	I forward a request in my servlet.
	After I commit the output, I perform the forwarding.
	Tomcat doesn't act conforming to the servlet 2.2 specification,
which specifies that it should throw the IllegalStateException.
	Peek the source:
	public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
		response.setContentType("text/html;charset=gb2312");
		PrintWriter out = response.getWriter();
		out.println("<html>");
		out.println("<head>");
		out.println("<title>Forward A Request</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>Just A Test!</h1>");
		out.println("</body>");
		out.println("</html>");
		out.flush();
		request.getRequestDispatcher("/forwarded.jsp").forward(request,response);
	}

----------
通信地址:南京市汉中路180号星汉大厦11楼<210029>
电    话:025-6799480-308
POSTADDR:11F XINGHAN BUILDING 180 HANZHONG ROAD NAN JING CHINA
 ZIPCODE:210029
     TEL:86-25-6799480-308


Re: I think that it is a bug.

Posted by "Craig R. McClanahan" <Cr...@eng.sun.com>.
Qian Weichun wrote:

> The bug lies here:
>         I forward a request in my servlet.
>         After I commit the output, I perform the forwarding.
>         Tomcat doesn't act conforming to the servlet 2.2 specification,
> which specifies that it should throw the IllegalStateException.
>         Peek the source:
>         public void doGet(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException {
>                 response.setContentType("text/html;charset=gb2312");
>                 PrintWriter out = response.getWriter();
>                 out.println("<html>");
>                 out.println("<head>");
>                 out.println("<title>Forward A Request</title>");
>                 out.println("</head>");
>                 out.println("<body>");
>                 out.println("<h1>Just A Test!</h1>");
>                 out.println("</body>");
>                 out.println("</html>");
>                 out.flush();
>                 request.getRequestDispatcher("/forwarded.jsp").forward(request,response);
>         }
>

Qian,

If Tomcat 3.2 were an implementation of the Servlet 2.3 specification, this would indeed be a spec violation, because the
2.3 spec specifically states that flushing the output stream returned by response.getOutputStream(), or the PrintWriter
returned by response.getWriter(), will cause the response to be committed.

In a servlet 2.2 environment, however, it is not defined whether or not a flush() call on the output stream or writer causes
the response to be committed.  Therefore, Tomcat's behavior (while perhaps unexpected) is within the bounds of acceptable
behavior.

If you want to ensure that the response has been committed, no matter what version of the servlet spec your container
implements, call response.flushBuffer() instead of out.flush().

Craig McClanahan