How can you serve a dynamically generated file w/ Tomcat & Java technology?

General Tech Technology & Software 2 years ago

0 2 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating

Posted on 16 Aug 2022, this text provides information on Technology & Software related to General Tech. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (2)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago

Having a hard time figuring out how to send dynamic data to a web-app's user without having to create a file & using a dynamic link to static content using java technology. (The main reasons for avoiding the file creation on disk are security and speed)

This is something I have previously done in python, but this web-app is intended to be strictly java technology for maintenance purposes.

After doing a few searches in google I haven't gotten any further. Has anyone done this before or has a good idea for a starting point?

profilepic.png
manpreet 2 years ago

You can output any kind of content type from a servlet. Just set the headers right, and dump to the body of the request the content you want to send.

response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment;filename=" + file);
// generate the content and send it to response.getOutputStream()

This will set the content-type to an excel file, and force the browser to download the file.

As a bonus, you can tell your servlet to listen to a specific path, and set the link with a real filename.

<servlet-mapping>
    <servlet-name>Exportservlet-name>
    <url-pattern>/export/*


request.getContextPath()%>/export/Myreport.xls">
    Myreport.xls

0 views   0 shares

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.