0

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.

2 Answers 2

1

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).

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, Can you point to any examples where this is done using an iframe?
Well you'd just submit the search normally, and when the page loads you'd submit the search form again to an iframe e.g. <iframe src='blank.html' name='excelIframe' /> (e.g. by setting the target-attribute of the search form to target='excelIframe' and the action of the form to 'excelServlet').
0

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

1 Comment

Unfortunately i cant be using cookies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.