title: Forward request from a filter
body: <p>I need to <em>forward my request</em> (to a jsp but I don't think it's matter) from an http.Filter
  if the URI of the original request pass some validation that my filter runs.</p>
  
  <p>I found this <a href="http://mail-archives.apache.org/mod_mbox/tomcat-users/200111.mbox/%3C20011110161313.A24324-100000@localhost%3E" rel="nofollow noreferrer">page that faced similar task</a> </p>
  
  <p>Still I need to figure the following: </p>
  
  <ol>
  <li><p>How can I get ServletContext in <code>doFilter()</code> method (in order to call forward API)  <code>getServletContext()</code> is not recignized</p></li>
  <li><p>Do I have to <code>call chain.doFilter()</code> before the forward, after the forward or not at all?
  In addition do I have to call <code>chain.doFilter()</code>  if my validation passed or only if it fails (because in this case I won't continue to forward my page)?</p></li>
  </ol>
  
  <p>This question actually continue <a href="https://stackoverflow.com/questions/3414733/how-to-redirect-url-to-jsp-in-web-xml">this thread</a>, 
  to be more obvious, the code could be something like:</p>
  
  <pre><code>public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
          if (request instanceof HttpServletRequest) {
              HttpServletRequest httpServletRequest = ((HttpServletRequest)request);
              String requestURI = httpServletRequest.getRequestURI();
              String contextPath = httpServletRequest.getContextPath();
              if (&lt;this is my implementation of the validation of this filter&gt;){                                      
                  getServletContext().getRequestDispatcher(
                  "MySpecific.jsp").forward(request,response);
  
              }
  
          }
          chain.doFilter(request,response);
  
      }
  </code></pre>