我的代码为:public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setCharacterEncoding("GBK");
PrintWriter out = response.getWriter();
if (!validator.equals(in_validateCode)){
out.println(" <script type=\"text/javascript\"> ");
out.println(" alert('验证码填写错误!') ");
out.println(" </script> ");
out.flush();
out.close();
                            response.sendRedirect("../index.jsp");
}}其中:out.flush(); out.close(); 和 response.sendRedirect("../index.jsp"); 好像不能同时使用,有 
java.lang.IllegalStateException: Response already committed
 
异常抛出但我在帮助文档中没有找到那里出的问题.请大家帮我指出问题所在,谢谢!!!!!

解决方案 »

  1.   

    试下看可不可以帮助你。
    request.getRequestDispatcher("/index.jsp").forward(request, response);
      

  2.   

    试下看可不可以帮助你。
    request.getRequestDispatcher("/index.jsp").forward(request, response);
      

  3.   

    还是不行,3楼,在我这里测试显示:
    可以完成跳转,但 alert('验证码填写错误') 没有看到!2楼,在我这里测试显示如果在 out.flush(); out.close(); 后面使用,就会有异常 java.lang.IllegalStateException: Cannot forward a response that is already committed 抛出如果在 out.flush(); out.close(); 前面使用,和 3楼 的问题一样,没有看到 alert('验证码填写错误')辛苦大家了, 谢谢 !!!!!!!
      

  4.   

    out.flush();时服务器会把请求头部分以及现成的其他response体刷到客户端,这里response置为commited,不能再改变其请求头, 而response.sendRedirect("../index.jsp"); 要改变location请求头,当然不行了.
      

  5.   

    out.print( " <script   language= 'javascript '> "); 
    out.print( "alert( '提交成功 '); "); 
    out.print( "window.location.href= '/XXX/yyy.jsp?aaa=78'; "); 
    out.print( " </script> "); 
      

  6.   

    先试试这个
    或者直接用ajax吧
    不用打印js代码了 
      

  7.   

    他们不能在一起用的,你想想看,你的serlvet,用的printwriter ,它就不能再用sendredirect了,你想实现什么样的功能?
      

  8.   

    out.close();加 response.reset();  试试 
    response.sendRedirect("../index.jsp"); 
      

  9.   

    out都关了   response.sendRedirect("../index.jsp"); 是要往客户端头信息写东西的 关掉了就写不过去了