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