title: Java servlet redirect between two different jersey REST services
body: <p>We have an existing Jersey REST service in our application(URL: /rest/*). 
  A sample URL looks like: <code>http://xxx:8080/app/rest/company/getdata</code></p>
  
  <p>Based on a <strong>property</strong> we need to redirect the REST call to another context(URL: /newrest/*):
  A sample URL would look like: <code>http://xxx:8080/app/newrest/company/getdata</code></p>
  
  <p>So, I added a servlet mapping in my web.xml.</p>
  
  <p>So, My web.xml looks like the below snippet.</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;servlet&gt;
      &lt;servlet-name&gt;ServletAdaptor&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;com.sun.jersey.config.property.packages&lt;/param-name&gt;
          &lt;param-value&gt;xxx.httpservice&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;ServletAdaptor&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;  
  &lt;filter&gt;
      &lt;filter-name&gt;secureRESTFilter&lt;/filter-name&gt;
      &lt;filter-class&gt;xxx.RESTSecurityFilter&lt;/filter-class&gt;
  &lt;/filter&gt;
  &lt;filter-mapping&gt;
      &lt;filter-name&gt;secureRESTFilter&lt;/filter-name&gt;
      &lt;url-pattern&gt;/rest/*&lt;/url-pattern&gt;
  &lt;/filter-mapping&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;NewAdaptor&lt;/servlet-name&gt;
      &lt;servlet-class&gt;com.sun.jersey.spi.container.servlet.ServletContainer&lt;/servlet-class&gt;
      &lt;init-param&gt;
          &lt;param-name&gt;com.sun.jersey.config.property.packages&lt;/param-name&gt;
          &lt;param-value&gt;xxx.newhttpservice&lt;/param-value&gt;
      &lt;/init-param&gt;
      &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;NewAdaptor&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/newrest/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>
  
  <p>I have written a filter(to check for the <strong>property</strong> on the ServletAdaptor servlet and I need to redirect to the NewAdaptor servlet, if needed. Also, the final response should be sent back by the Servlet Adaptor servlet using the response from NewAdaptor servlet, if necessary(based on the property). </p>
  
  <p>I need directions for solving this issue. Please help.</p>
  
  <p>The dofilter method in my filter class looks like:</p>
  
  <pre><code>@Override
  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {     
      HttpServletRequest request = (HttpServletRequest) req;    
      String productMode = "b";//hardcoded for this example    
      String url = request.getRequestURL().toString();
      if(productMode.equals("a")){
          chain.doFilter(req, res);
      }else{
          url=url.replace("rest", "newrest");
          req.getRequestDispatcher(url).forward(req, res);
      }
  
  }
  </code></pre>
  
  <p>But the RequestDispatcher does not seem to forward the request to the new url.</p>