为了测试:request.getReqestDispatcher("test02.jsp").forward(request,response);
写了个程序,一直出现如题异常,最终解决:
在调用它们之前,都不能有内容已经被实际输出到了客户端。如果缓冲区中已经有了一些内容,这些内容将被从缓冲区中清除。
因为myeclipse 自动生成的代码忘记删除,所以导致该错误。
api:
forward
void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException
-
Forwards a request from a servlet to another resource (servlet, JSP file, or HTML file) on the server. This method allows one servlet to do preliminary processing of a request and another resource to generate the response.
For a
RequestDispatcher
obtained viagetRequestDispatcher()
, theServletRequest
object has its path elements and parameters adjusted to match the path of the target resource.forward
should be called before the response has been committed to the client (before response body output has been flushed). If the response already has been committed, this method throws anIllegalStateException
. Uncommitted output in the response buffer is automatically cleared before the forward.The request and response parameters must be either the same objects as were passed to the calling servlet's service method or be subclasses of the
ServletRequestWrapper
orServletResponseWrapper
classes that wrap them. -
-
Parameters:
-
request
- aServletRequest
object that represents the request the client makes of the servlet -
response
- aServletResponse
object that represents the response the servlet returns to the client
Throws:
-
ServletException
- if the target resource throws this exception -
IOException
- if the target resource throws this exception -
IllegalStateException
- if the response was already committed
-