pkr13
pkr13

Reputation: 107

Return an Excel file from Java Servlet along with HTML content

Using java servlet i need to do a back-end DB query and populate the results in an excel file and provide to client user. I have a working code of downloading Excel by setting the HtmlServletResponse object's contentType and Header like below:

    response.setContentType("application/vnd.ms-excel; charset=utf-8");
    response.setHeader("Content-Disposition", "attachment; filename="+ "Report" + ".xls");

But my question is in addition to providing this Excel sheet as a download i also need to send the 'initial search criteria' selected by the user as a HTML. And for html, i need to set the content type like below.

 response.setCharacterEncoding("UTF-8");
 response.setContentType("text/html");

I think it's not possible to set the content type to 2 different values. How to solve this problem?

To phrase the problem in a different way -- "I have a HTML search form based on which user selects a search criteria - While providing the results in an excel save as file, i need to repopulate the same html so that search criteria selected by user is not lost".

I am new to Servlets and not sure if this is very straight forward. Thanks for the help.

Upvotes: 0

Views: 1416

Answers (2)

fiffy
fiffy

Reputation: 840

You can not respond to the Service Request with more than one response. You'd need to show the search page with the search criterea and have the search page create the excel-file (in an iframe or something).

Upvotes: 1

pomkine
pomkine

Reputation: 1615

You can try to save your search criteria in cookies. Here is some info about it http://www.journaldev.com/1907/java-servlet-session-management-tutorial-with-examples-of-cookies-httpsession-and-url-rewriting

Upvotes: 0

Related Questions