title: Using sendRedirect inside a servlet forwarded with request dispatcher
body: <p>I have a filter that implements a custom convention for loading servlets and JSPs. In that convention I am using the following code to include the servlet:</p>
  
  <pre><code>servletContext
      .getRequestDispatcher( uriDispatcherLocator.getServletLocation( uri ) )
      .include( request, response );
  </code></pre>
  
  <p>and the following code to include the JSP (in the same filter):</p>
  
  <pre><code>servletContext
      .getRequestDispatcher( "/index.jsp" )
      .include( request, response );
  </code></pre>
  
  <p>Everything works fine, the servlet executes, then it includes the JSP and some irrelevant custom rules take place.
  As you can see, at the very moment I include a servlet with request dispatcher <strong>I cannot send an http header response to the client</strong>.
  The problem is that I want the servlet to have full control of the response as if it was called from inside the filter (because the filter will do nothing else than dinamically mapping the servlets according to their respective Class/JSP location in the project file system).
  I can use <code>.forward()</code> instead of <code>.include()</code>, but if I do I will not be able to include a JSP after the servlet has been executed.</p>
  
  <p>So, how would I allow the servlet to execute the code below when being included through a filter via RequestDispatcher interface?</p>
  
  <pre><code>response.sendRedirect( "/somePath" );
  </code></pre>
  
  <p>No Javascript hacks, I am willing to send the proper HTTP response from the server to make the browser behave correctly.</p>
  
  <p>--</p>
  
  <p>EDIT:
  In other words:</p>
  
  <p>I want to change the headers sent to the client from INSIDE an included servlet by using RequestDispatcher, but the docs states:</p>
  
  <pre><code>The included servlet cannot change the response status code or set headers; any attempt to make a change is ignored.
  </code></pre>