title: java.lang.IllegalStateException: Cannot call sendRedirect() after the response has been committed
body: <p>For two days I have tried find out what went wrong. I read here that i should add a return into the code, and I did it, and i still get </p>
  
  <pre><code>java.lang.IllegalStateException: Cannot call sendRedirect() 
       after the response has been committed, Error.
  </code></pre>
  
  <p>How can I solve this problem?</p>
  
  <p>It happens every time I connect to the database. This is the connect method:</p>
  
  <pre><code>&lt;%!
  
  public  void connect()
  {
          try {
              Class.forName("com.mysql.jdbc.Driver");
              String dbURL = "jdbc:mysql://localhost:3306/moti";
              String user = "root";
              String password = "j3o4h5n6y7";
              con =  DriverManager.getConnection(dbURL, user, password);  
              statement = con.createStatement();
          }
          catch(Exception ex) {
              throw new Error(ex);
          }  
  }
  %&gt;
  </code></pre>
  
  <p>like in this code block  :</p>
  
  <pre><code>            String post = request.getParameter("send");
              if(post != null )
              {
                      connect();
                      statement.execute(add);
                      con.close();
                      response.sendRedirect("fourm.jsp");
                      return;
  
              }
  </code></pre>
  
  <p>but in this code block its work perfectly  :</p>
  
  <pre><code>    String back = request.getParameter("retrun");
  
      if(back != null)
      {
  
          response.sendRedirect("fourm.jsp");
          return;
      }       
  </code></pre>