title: Cannot call sendRedirect() after the response has been committed
body: <p>i have a filter that checks if a session exists. If it doesn't exist i redirect the user, with a <code>response.sendRedirect()</code>, to the login page. </p>
  
  <p>In some pages it works, in others it doesnt'. I get this error <code>Cannot call sendRedirect() after the response has been committed</code>.</p>
  
  <p>It seems that if i delete some parts of code like <code>&lt;%=variable%&gt;</code> in my jsp pages it works.
  But, as i said, in other jsp pages it works even if i have code like <code>&lt;%=variable%&gt;</code>.
  I really can't understand what's the problem.</p>
  
  <p>I read some posts similar on this forum but i didn't find a solution.</p>
  
  <p>This is my filter </p>
  
  <pre><code>public class SessionFilter implements Filter {
  
      public void destroy() {}
  
      public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
  
          try {
              HttpServletRequest request = (HttpServletRequest) req;
              HttpServletResponse response = (HttpServletResponse) res;
              String url = request.getServletPath();
  
              HttpSession session = request.getSession(false);
  
              /*caso in cui il webserver non abbia ancora creato la sessione*/
              if (null == session) {
                  reindirizza = true;
              } else {
                  /*caso in cui il webserver abbia creato la sessione e l'utente  sia loggato*/
                  if(session.getAttribute("loggato")!=null &amp;&amp; session.getAttribute("loggato").equals("si")) {
                      reindirizza = false;
                  }
                  /*caso in cui il webserver abbia creato la sessione e l'utente  non sia loggato*/
                  else {
                      reindirizza = true;
                  }
              }
  
              chain.doFilter(req, res);
              if(reindirizza) {
                  response.sendRedirect("Gestione.jsp?message=Sessione scaduta o non valida. Effettuare il Login");
              }
          } catch(Exception e) {
              System.out.println(e.getMessage());
          }
      }
  
      public void init(FilterConfig config) throws ServletException { }
  }
  </code></pre>