title: doFilter method on java - null pointer exception on filterchain when session is invalidate
body: <p>this is my java filter (doFilter method):</p>
  
  <pre><code>public void doFilter(ServletRequest request, ServletResponse response,
          FilterChain filterChain) throws IOException, ServletException {
  
      HttpServletRequest req = (HttpServletRequest) request;
      HttpServletResponse res = (HttpServletResponse) response;
  
      String uri = req.getRequestURI();
      // context.log("Requested Resource::"+uri);
  
      if ((!req.isRequestedSessionIdValid()
              &amp;&amp; (uri.endsWith("Login.jsp") || uri.endsWith("LoginServlet")) || 
              req.isRequestedSessionIdValid())) {
  
  
          // pass the request along the filter chain
           try {
          filterChain.doFilter(req, res);
  
           }catch (Exception e) {
              // TODO: handle exception
               e.printStackTrace();
              res.sendRedirect("Logout");
          }
  
      } else {
  
          res.sendRedirect("Logout");
  
          // context.log("Unauthorized access request");
      }
  
  }
  
  public void destroy() {
  }
  }
  </code></pre>
  
  <p>I use this filter because i need to controll if the session is valid or not;
  this is useful in case of , after logout, if the back button of browser is pressed, automaticly it redirect the client on Login.jsp.
  <strong>The problem is on try catch module</strong>: infact without it, when i try to press back button the JVM give me the follow error:</p>
  
  <pre><code>org.apache.jasper.JasperException: java.lang.NullPointerException
  at  org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:534)
  at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:457)
  at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
  at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at com.connection.filter.ServletFilter.doFilter(ServletFilter.java:41)
  at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
  at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
  at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
  at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
  at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
  at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
  at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
  at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
  at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
  at java.lang.Thread.run(Thread.java:744)
  Caused by: java.lang.NullPointerException
  at org.apache.jsp.Home_jsp._jspService(Home_jsp.java:155)
  at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
  at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
  ... 22 more
  </code></pre>
  
  <p>i think that this happen because the filterchain is not present, when there are a not valid session,  but how can manage this problem? i think that the "try catch" module is not a good solution.
  thanks</p>