title: Servlet - export response to Excel file
body: <p>I'm unable to export the response from a servlet to an excel file. Please see the code below: </p>
  
  <p>Test.java:</p>
  
  <pre><code> @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
  response.setContentType("text/html");
  out = response.getWriter();
  out.print("&lt;form name=\"test\" method=\"post\" action=\"Export\"&gt;");
  out.print("&lt;table border=\"1\" cellpadding=\"3\" bordercolor='black'");
  out.print("&lt;tr&gt;");
  out.print("&lt;td&gt;1&lt;/td&gt;");
  out.print("&lt;td&gt;hello how are you?&lt;/td&gt;");
  out.print("&lt;/tr&gt;");
  out.print("&lt;/table&gt;");
  out.print("&lt;td&gt;&lt;input type=\"submit\" name =\"submit1\" value=\"Export To Excel\"&gt;&lt;/td&gt;");
  out.print("&lt;/form&gt;");
  </code></pre>
  
  <p>The submit button when clicked produce an excel sheet which doesn't contain any values. See the <code>Export.java</code> which is called when submit button is clicked. </p>
  
  <p>Export.java</p>
  
  <pre><code>public class Export extends HttpServlet {
  
  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
      String submit1 = request.getParameter("submit1");
     if (submit1 != null) {
          response.setContentType("application/vnd.ms-excel");
          response.setHeader("Content-Disposition", "attachment; filename=users.xls");
      }
  }
  }
  </code></pre>
  
  <p>Also, it has been observed that if i write the below code in <code>Test.java</code>, its working fine and the excel sheet does contain the table values.</p>
  
  <pre><code>response.setContentType("application/vnd.ms-excel");
  response.setHeader("Content-Disposition", "attachment; filename=users.xls");
  </code></pre>
  
  <p>Is there any way to forward the output of <code>Export.java</code> to <code>Test.java</code> when the submit button is clicked. </p>