With Scott's inputs, my solution with code snippets.
1. From the UI, submit the action to Sling Servlet
<form name="importFileForm" method="get" action="/services/getData"> <input type="submit" title="Submit" value="Submit" name="bttnAction"> </form>
2. Your Servlet class
public class TTIGetServlet extends SlingAllMethodsServlet { @Override protected void doGet(SlingHttpServletRequest request,SlingHttpServletResponse response) throws ServletException,IOException { ... ... String serviceurl = <<< your webservice url>>> HttpClient httpclient = HttpClients.custom().build(); generateFile(serviceurl, httpclient, request, response); RequestDispatcher dispatcher = request.getRequestDispatcher("/content/ttii/en/importfiletest.html"); dispatcher.forward(request, response); } }3. Generate File method that pops up the file download on browser
public static void generateFile(String serviceurl, HttpClient httpclient, SlingHttpServletRequest httpRequest, SlingHttpServletResponse httpResponse) throws ClientProtocolException, IOException { HttpResponse response; HttpGet httpGet = new HttpGet(serviceURL); // Makes the call to WebService response = httpclient.execute(httpGet); // CORE LOGIC if (response!=null) { ContentType contentType = ContentType.getOrDefault(response.getEntity()); String mimeType = contentType.getMimeType(); if (mimeType.equals(MIMETYPE_JSON)) { // Out of context here... } else { // SHOW THE FILE ServletOutputStream sos = httpResponse.getOutputStream(); httpResponse.setContentType("application/vnd.ms-excel"); httpResponse.setHeader("Content-Disposition", "attachment;filename=test.xls"); BufferedHttpEntity buf = new BufferedHttpEntity(response.getEntity()); InputStream istream = buf.getContent(); sos.write(FileHelper.writeFiles(istream)); sos.flush(); } } }