title: Session Checking with Filter and Redirecting
body: <p>I am trying to do login operation but when I am using filter for checking whether session is valid or not then I got the exception as :-  <code>java.lang.IllegalStateException</code>, Cannot create a session after the response has been committed . </p>
  
  <p>Myservlet filter code is :-</p>
  
  <pre><code> public class SessionFilter implements javax.servlet.Filter 
      {
  
  private ArrayList&lt;String&gt; urlList;
  
       public void destroy() {
  
  }
  
  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 
      throws IOException, ServletException {
  
  HttpServletRequest request = (HttpServletRequest) req;
      HttpServletResponse response = (HttpServletResponse) res;
      String url = request.getServletPath();
      boolean allowedRequest = false;
  
  
      if(urlList.contains(url)) {
          allowedRequest = true;
  
      }
  
      if (!allowedRequest) {
          HttpSession session = request.getSession(false);
          if (null == session) {
              response.sendRedirect("Login.jsp");
          }
      }
  
    chain.doFilter(req, res);
  
  
  
  }
  
  
  public void init(FilterConfig fConfig) throws ServletException {
  
      String urls = fConfig.getInitParameter("avoid-urls");
  
      //StringTokenizer token = new StringTokenizer(urls, ",");
  
      StringTokenizer token = new StringTokenizer(urls);
  
      urlList = new ArrayList&lt;String&gt;();
  
      urlList.add(token.nextToken());
  
      /*while (token.hasMoreTokens()) {
          urlList.add(token.nextToken());
  
      }*/
  }
  
  }
  
  
  
    Filter mapping in web.xml is :-
  
  &lt;filter&gt;
     &lt;filter-name&gt;SessionFilter&lt;/filter-name&gt;
     &lt;filter-class&gt;Filter.SessionFilter&lt;/filter-class&gt;
  
      &lt;init-param&gt;
        &lt;param-name&gt;avoid-urls&lt;/param-name&gt;
        &lt;param-value&gt;Login.jsp,&lt;/param-value&gt;
      &lt;/init-param&gt;
  &lt;/filter&gt;
  
  &lt;filter-mapping&gt;
  
      &lt;filter-name&gt;SessionFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
  
  &lt;/filter-mapping&gt;
  </code></pre>
  
  <p>Now when I requests for <code>login.jsp</code> then i got the this exception . Please help i am 
    getting stuck  on my project since last 2 days here.</p>