title: Error handling by redirection in spring download file controller
body: <p><a href="https://stackoverflow.com/questions/5673260/downloading-a-file-from-spring-controllers">This</a> question nicely explains on how to write download file controllers in spring. <a href="https://stackoverflow.com/questions/17001185/pass-hidden-parameters-using-response-sendredirect">This</a> question nicely explains that Post request cannot be sent using response.sendRedirect()</p>
  
  <p>I would like the user to be redirected to the same page with an error about what caused file download error. Here is the workflow</p>
  
  <ol>
  <li>User hits www.abc.com/index [controller has mapping /index.jsp and return ModelAndView]</li>
  <li>At this page, we have a file download which has URL www.abc.com/download?resource_id=123. [controller has mapping /download and returns void]</li>
  <li>When there is error in file download, user should be redirected to www.abc.com/index with some error display.</li>
  <li>When there is no error in file download, user stays at the same page and file download dialog appears.</li>
  </ol>
  
  <p>Following is a snippet for forwarding:</p>
  
  <pre><code>@RequestMapping(/download)
  public void execute(@RequestParam(value = "resource_id" required = true) final String resource, final HttpServletRequest request, final HttpServletResponse response) {
      try {
          //some processing
      } catch {
          RequestDispatcher dispatcher = request.getRequestDispatcher("/index" + "?is_downloaded=false");
          dispatcher.forward(request, response)
      }
  }
  
  @RequestMapping(/index)
  public void execute(@RequestParam(value = "is_downloaded" required = false) final String isDownloaded, final HttpServletRequest request) {
      //do stuff here
  }
  </code></pre>
  
  <p>Step 3 is the problem. </p>
  
  <ul>
  <li>Using forwarding changes the URL to the download URL of the report on error.</li>
  <li>Using redirect as response.sendRedirect() with hidden parameters is impossible would not modify the URL at all.</li>
  <li>Using redirect as response.sendRedirect() with hidden parameters and would introduce "?is_downloaded=false" at the end of URL</li>
  </ul>
  
  <p>Can anyone tell a workaround for this.</p>