response.setRedirect之前不要输出,比如不要有out.println()这样的东西

解决方案 »

  1.   

    <%@ include file="/contentType.jsp"%>
    <%@ page errorPage= "../error/errorPage2.jsp" %>
    <%@ page import= "com.lilytech.util.*"%>
    <%@ page import= "java.sql.*" %>
    <%@ page import= "java.io.*" %>
    <%@ page import= "java.util.*" %>
    <jsp:include page="left.htm" flush="true"/>
    <%
        String function=request.getParameter("function");
        if (function!=null) {
            if (function.equals("password")) {
                System.out.println("function:"+function);
        String name=request.getParameter("name"); System.out.println("name:"+name);
        String question=request.getParameter("question");   System.out.println("question:"+question);
                
                SqlBean sqlBean = new SqlBean();
                try {
                    Connection con = sqlBean.getConnection();
                    Statement stmt = con.createStatement();
                    String sqlStr = "select PWD from SiteUserInfo where NAME='"+ name +"' and QUESTION='"+ question +"'";
                    ResultSet rset = stmt.executeQuery(sqlStr);
                    String password = "";
                    
                    if (rset.next())
                        password = rset.getString("PWD");   
                        System.out.println("finallyPass:"+password+"|");
                        session.setAttribute("password",password);
                        session.setAttribute("name",name);
                    rset.close();
                    stmt.close();
                    
                    
                 if ("".equals(password)) {
                     System.out.println("fail!!!");
                     response.sendRedirect("passwordfail.htm");
                 }
                 else {
                     System.out.println("success!!!");
                     String url="forgetsuccess.jsp?password="+password+"&name="+name;
                     System.out.println("url:"+url);
                     response.sendRedirect(url);
                 }
                 con.close();                                             
                }
                catch (Exception e) {
                    System.out.println(e.getMessage());
                    e.printStackTrace();
                }
        finally {
            sqlBean.close();
        }            
            }
        }
    %>
     <!-- InstanceBeginEditable name="work" -->
    <html>
    <head>
    <title>用户注册--阅读条款</title>
    <script language=javascript></script>
    </head>
    <body>
    <form name=form1 method=post action="forgetPassword.jsp"> 
          <table width="100%" border="0" cellspacing="2" cellpadding="2">
            <tr> 
              <td height="110" colspan="2"><object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="590" height="120">
                  <param name="movie" value="../images/1.swf">
                  <param name="quality" value="high">
                  <embed src="../images/1.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="590" height="120"></embed></object></td>
            </tr>
            <tr> 
              <td colspan="2">取回密码</td>
            </tr>
            <tr> 
              <td width="40%" height="31" bgcolor=""><div align="right">姓&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名:</div></td>
              <td width="57%"><div align="left"> 
              <input name="name" type="text" class="textfield" size="10">
              </div></td>
            </tr>
            <tr>
              <td width="40%" height="31" bgcolor=""><div align="right">密码提示问题:</div></td>
              <td width="57%"><div align="left"> 
              <input name="question" type="text" class="textfield" size="10">
              </div></td>
            </tr>
            <tr> 
              <td width="11%" valign="top"></td>
              <td width="89%" valign="top">
              <input name="Submit" type="image" value="Submit" src="../images/ok.gif" width="65" height="20" border=0>&nbsp;&nbsp;
              <a href="javascript:document.form1.reset()"><img src="../images/cz.gif" width="65" height="20" border="0"></a></td>
            </tr>
            <tr valign="top"> 
              <td colspan="2"></td>
            </tr>
            <input type=hidden name="function" value="password">
          </table>
    </form>
    </body>
    </html>
          <!-- InstanceEndEditable -->
    <%@ include file="buttom.htm"%>
      

  2.   

    System.out.println(...);算是out.println()吗
      

  3.   

    我把System.out.println();去掉了,还是一样的错
      

  4.   

    System.out.println不算,你看看这个贴在或许对你有些帮助
      

  5.   

    抱歉,忘了贴地址了
    http://forum.java.sun.com/thread.jspa?threadID=567596&messageID=2803252
      

  6.   

    我这里可以上的啊,贴给你算了
    -----------------------------
    This topic has 12 replies on 1 page.   
     response.sendRedirect() gives major problem.
    Author: Kausar.Sarita   Oct 31, 2004 10:38 PM   
     Hi,I have developed a jsp application using Tomcat4.1.24 I am using response.sendRedirect(URL); to redirect my jsp page to another jsp page. 
    The problem is the page displayed after redirection is not complete, it halts abruptly at any html tag, hence only a part of page is displayed. Refreshing the page doesn't help. The log shows IllegalStateException.
    I tried using document.form.submit() but to no effect.Is there any problem with response.sendRedirect() ?Thanking in advance,
    Sarita. Re: response.sendRedirect() gives major problem.
    Author: ashish_248   Nov 1, 2004 2:34 AM (reply 1 of 12)   
     Illegal state exception with serverside technologies happens when something is wrtten on response stream after the request has been redirected/forwarded to another page
    Check if you are doing response.getWriter().write(...) or out.println(..) after the redirection
     Re: response.sendRedirect() gives major problem.
    Author: Kausar.Sarita   Nov 1, 2004 3:51 AM (reply 2 of 12)   
     Yes there is a fragment that has to be added ie.<%@ include file="/jspf/pageHalfTop.jsp" %><%-- ***** Visible of code **************************************************************** --%><%
    if(condition)
    {
    response.sendRedirect(nextPageUrl);
    }
    else
    {
    out.println(" a b c ");
    }%>
    <%-- ***** End of code **************************************************************** --%><%@ include file="/jspf/pageHalfBottom.jsp" %>Only a part of next page is displayed. i.e it abrubtly stops at any html tagThanks,
    Sarita
     Re: response.sendRedirect() gives major problem.
    Author: mubarak79   Nov 1, 2004 4:19 AM (reply 3 of 12)   
     try to increase the page buffer attribute.
    Sample:
    <%@ page buffer="25kb" %>
     Re: response.sendRedirect() gives major problem.
    Author: Kausar.Sarita   Nov 1, 2004 10:46 PM (reply 4 of 12)   
     I tried increasing the buffer size but to no effect.Is there any specific reason to why this is happening?Thanks,
    Sarita.
     Re: response.sendRedirect() gives major problem.
    Author: Nildu   Nov 2, 2004 1:05 AM (reply 5 of 12)   
     Hi,Please check whether you are using jsp:forward action tag before redirecting the page. Because internally jsp:forward flushes and commits the response and if you try to write something after that, it will throw IllegalstateException.Hope this may helpNildu.
     Re: response.sendRedirect() gives major problem.
    Author: Kausar.Sarita   Nov 2, 2004 1:44 AM (reply 6 of 12)   
     No I am not using any jsp:forward action tag. 
    But when I remove any tool tips or reduce data displayed in any fragment then the page gets displayed properly.But what is the relation of the amount of data displayed on screen and the page display ?Thanks,
    Sarita 
     Re: response.sendRedirect() gives major problem.
    Author: Kausar.Sarita   Nov 2, 2004 5:00 AM (reply 7 of 12)   
     Is there a rule that response.sendRedirect() should not be used between lines of code. Thanks ,
    Sarita Re: response.sendRedirect() gives major problem.
    Author: bsampieri   Nov 2, 2004 6:22 AM (reply 8 of 12)   
     The rule is that you can't use sendRedirect if a response has already been committed. In general, you prevent that by determining as soon as possible in the page whether you will need to redirect or not, preferably before you let any of the page's HTML be written out. Assuming auto-flush is disabled (default) then you only have x KB of buffer space, so if you start writing out the HTML code, and decide later to redirect, it can be too late.You can redirect after that, if you can be sure Javascript is enabled on the client. You can write JS code into the page to redirect...<script language="JavaScript">
    document.location = '<%= theRedirectURL %>';
    </script>
      

  7.   

    Re: response.sendRedirect() gives major problem.
    Author: Kausar.Sarita   Nov 2, 2004 8:35 PM (reply 9 of 12)   
     Thank you for a very clean explanation, it has made things more clearer.I have used the below javascript instead of response.sendRedirect()<script language="javascript">
    doucment.form_name.submit();
    </script>but this is not working consistently for all jsp pages. The problem occurs irrespective of the javascript also.Thanks,
    Sarita
     Re: response.sendRedirect() gives major problem.
    Author: Balasubramanyam.Appana   Nov 3, 2004 3:32 AM (reply 10 of 12)   
     please forward ur jsp file once ..then i will try to solve ur problem
     Re: response.sendRedirect() gives major problem.
    Author: bsampieri   Nov 3, 2004 9:19 AM (reply 11 of 12)   
     are you sure that the form submit code you're writing has been included after the end of the form in the page?One thing you could try is what I posted in response #3 here:
    http://forum.java.sun.com/thread.jsp?forum=45&thread=550658
     Re: response.sendRedirect() gives major problem.
    Author: Kausar.Sarita   Nov 7, 2004 11:48 PM (reply 12 of 12)   
     Sorry for replying late, i have pasted the jsp code.
    <%@ page  session = "false"%>
     
    <%@ page import="java.util.*" %>
    <%@ page import="dataAccess.Department"%>
     
    <%-- ***** End of imports *************************************************************** --%>
     
    <%
    String pageId = "ManageDepartments";
    int mainMenuOption = 6;
    int subMenuOption = 3;
    String pageCaption = "Manage Departments";
    %>
     
    <%@ include file="/jspf/appendUrl.jsp" %>
     
    <html>
    <head>
    <%@ include file="/jspf/css.jsp" %>
    </head>
     
    <body>
     
    <%
    logMaker.write(priUserId, pageId, "main", "in", "-");
    %>
     
    <%-- ************************************************************************************ --%>
     
    <%@ include file="/jspf/pageHalfTop.jsp" %>
     
    <%-- ***** Start of code **************************************************************** --%>
     
    <jsp:useBean id="viewBean" class="view.admin.MyDepartments" />
     
    <% String nextPageUrl = "manageDepartment.jsp"+appendUrl;%>
     
    <%-- ***** Visible body ***************************************************************** --%>
    <form name="redirectForm" method="post">
     
    <%
    viewBean.onLoad(priUserId); // Display all departments of the administrator
     
    if(viewBean.deptVec.size() > 1)
    {
    Iterator adminDeptItr = viewBean.deptVec.iterator();
     
    if(adminDeptItr.hasNext())
    { %>
    <table border=1 cellpadding=0 cellspacing=0>
    <tr>
    <th>&nbsp;Department&nbsp;</th>
    </tr> <%
    while(adminDeptItr.hasNext())
    {
    Department deptBean = (Department)adminDeptItr.next(); %>
    <tr>
    <td>
    &nbsp;
    <a class="noLine" href="<%= nextPageUrl%>&deptCode=<%= deptBean.code%>">
    <%= deptBean.name%></a>
    &nbsp;
    </td>
    </tr>
    <%
    } %>
    </table>
    <%
    }
    else
    {
    out.println("<b> Currently the are no Departments</b>");
    }
    }
    else
    {
    if((viewBean.deptVec.size() == 1))
    {
    Iterator adminDeptItr = viewBean.deptVec.iterator();
    Department deptBean = (Department)adminDeptItr.next();
    response.sendRedirect(nextPageUrl+"&deptCode="+deptBean.code;
    }
    else
    {
    out.println("<b> Currently the are no Departments</b>");
    }
    }%>
     
    </form>
     
    <%-- ***** End of code **************************************************************** --%>
     
    <%@ include file="/jspf/pageHalfBottom.jsp" %>
      

  8.   

    呵呵,sendredirect()之后加个return就行了,试试
      

  9.   

    你的contentType.jsp是什么?
    如果是非text/html的二进制content type,那么你就不能用out.print