• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

A question about a program given in Head First Servlets and JSP book

 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I'm learning servlets and JSP from "Head First Servlets and JSP" .In chapter 4 I came across an example which illustrate use of output stream by simply downloading a (jar) file .


I tried and understood the concept but what surprised me is that while downloading It didn't recognize the format(file extension .jar) instead of that it displays the name which is given in url-pattern is displaying (****.do)


This what that program does :
Simply getting the input stream of that jar file and writing it to the response's output stream

I think that's why it behaves like that .

What I understood is correct ?

I want it should recognize the file name and format when downloaded .

How can I do that ?

Thanks in advance



 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The servlet should set the Content-Disposition header; something like this should do the trick:

response.setHeader("Content-Disposition", "attachment; filename=\"myFooBar.jar\"");
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ulf Dittmer wrote:The servlet should set the Content-Disposition header; something like this should do the trick:

response.setHeader("Content-Disposition", "attachment; filename=\"myFooBar.jar\"");




What does this code do? I mean why this value attachment; filename=\"myFooBar.jar\"?

I would like to know what exactly is happening when this value is entered. How does it affect and what does it do?
 
Greenhorn
Posts: 6
Netbeans IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Karthick,

Whenever you do a response.setHeader, you are sending some instruction to the browser (which initiated the request). In this case with "Content-Disposition" you are advising the browser how to handle the content being streamed to it.

Ulf Dittmer's code says that the browser should treat the response body as an attachment and that it's name to be shown in the File Download dialog is "myFooBar.jar"

Hope this helps!
 
karthick Soundararaj
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

hareendran dileep wrote:Karthick,

Whenever you do a response.setHeader, you are sending some instruction to the browser (which initiated the request). In this case with "Content-Disposition" you are advising the browser how to handle the content being streamed to it.

Ulf Dittmer's code says that the browser should treat the response body as an attachment and that it's name to be shown in the File Download dialog is "myFooBar.jar"

Hope this helps!



Thank you.. Now , how do you find these kind of values and header names.. Yes, googling can give us 100's of pages. But still, how does someone himself find the header needed for a particular action?
 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HTTP Specification
 
Parthiban Malayandi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ulf Dittmer ,

Thanks for your reply for my question which helped me to find the answer .
 
reply
    Bookmark Topic Watch Topic
  • New Topic