Categories

See More
Popular Forum

MBA (4887) B.Tech (1769) Engineering (1486) Class 12 (1030) Study Abroad (1004) Computer Science and Engineering (988) Business Management Studies (865) BBA (846) Diploma (746) CAT (651) B.Com (648) B.Sc (643) JEE Mains (618) Mechanical Engineering (574) Exam (525) India (462) Career (452) All Time Q&A (439) Mass Communication (427) BCA (417) Science (384) Computers & IT (Non-Engg) (383) Medicine & Health Sciences (381) Hotel Management (373) Civil Engineering (353) MCA (349) Tuteehub Top Questions (348) Distance (340) Colleges in India (334)
See More

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

General Tech Technology & Software
Max. 2000 characters
Replies

usr_profile.png
NeelKamal Jha

User

( 4 months 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?

usr_profile.png
Garry Buttler

User

( 4 months 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>Export</servlet-name>
    <url-pattern>/export/*</url-pattern>
</servlet-mapping>

<a href="<%=request.getContextPath()%>/export/Myreport.xls">
    Myreport.xls
</a>

what's your interest


forum_ban8_5d8c5fd7cf6f7.gif