aspose file tools*
The moose likes Servlets and the fly likes A question about a program given in Head First Servlets and JSP book Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "A question about a program given in Head First Servlets and JSP book" Watch "A question about a program given in Head First Servlets and JSP book" New topic
Author

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

Parthiban Malayandi
Ranch Hand

Joined: Apr 15, 2008
Posts: 70
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
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
The servlet should set the Content-Disposition header; something like this should do the trick:

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


Ping & DNS - updated with new look and Ping home screen widget
karthick Soundararaj
Greenhorn

Joined: Mar 09, 2009
Posts: 26
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?


A java addict
hareendran dileep
Greenhorn

Joined: May 07, 2009
Posts: 6

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

Joined: Mar 09, 2009
Posts: 26
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

Joined: Jan 10, 2002
Posts: 60050
    
  65

HTTP Specification


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Parthiban Malayandi
Ranch Hand

Joined: Apr 15, 2008
Posts: 70
Hi Ulf Dittmer ,

Thanks for your reply for my question which helped me to find the answer .
 
wood burning stoves
 
subject: A question about a program given in Head First Servlets and JSP book
 
Similar Threads
response object - Body Content
Tag files extensions
ibm websphere test 157 for websphere studio application developer 4.0.3 for windows
157 Question Help and Resourses
IBM Exam 157 - Questions and Answers (Sample Exam)