• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Parthiban Malayandi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • 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



 
Ulf Dittmer
Rancher
Posts: 42967
73
  • Mark post as helpful
  • send pies
  • 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\"");
 
karthick Soundararaj
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • 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?
 
hareendran dileep
Greenhorn
Posts: 6
Fedora Java Netbeans IDE
  • Mark post as helpful
  • send pies
  • 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
  • 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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64832
86
IntelliJ IDE Java jQuery Mac Mac OS X
 
Parthiban Malayandi
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ulf Dittmer ,

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