File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes Servlet Code to download Jar - HFJS Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "Servlet Code to download Jar - HFJS" Watch "Servlet Code to download Jar - HFJS" New topic
Author

Servlet Code to download Jar - HFJS

Vishal Kukreja
Greenhorn

Joined: Feb 22, 2012
Posts: 9
Hi,

I am trying below code from HFJS Chap-4, where client get Jar file on click of download.

Here problem is, when I click on Download button, it opens save dialog box with file name as of servlet name(selectCoffee.do) instead of javaQuery.jar.

HTML code:


Servlet code:


File javaQuery.jar is located under webapps\ResponseJar

Please help!
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1714
    
  25

Hi Vishal,

How does you web.xml look like? What is the structure of your .war file?

Regards,
Frits
Vishal Kukreja
Greenhorn

Joined: Feb 22, 2012
Posts: 9
Frits,

below is the code in web.xml


and directory structure of this webapp is

Tomcat_HOME
--\webapps\ResponseDemo\javaQuery.jar -- file for download
--\webapps\ResponseDemo\WEB-INF\web.xml --DD
--\webapps\ResponseDemo\WEB-INF\classes\com\example\web\CoffeeSelect.class --servlet class

and I'm directly trying to run it with
http://localhost:8080/ResponseDemo/selectCoffee.do
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1714
    
  25

Ok, everything looks fine.

Just change the .do into .jar and you will see that it works. (after saving open the file in winzip or similar program)

Regards,
Frits
Vishal Kukreja
Greenhorn

Joined: Feb 22, 2012
Posts: 9
Thanks Frits,
It works.

Can you please tell me instead of file name as javaQuery.jar why it is prompting to save as servlet name.
Frits Walraven
Creator of Enthuware JWS+ V6
Bartender

Joined: Apr 07, 2010
Posts: 1714
    
  25

Can you please tell me instead of file name as javaQuery.jar why it is prompting to save as servlet name.

It is actually prompting to save the selectCoffee.do because of the servlet mapping. The browser only knows that you are doing a GET request to the URL http://localhost:8080/ResponseDemo/selectCoffee.do and sees that it is returning a file (because of the content-type). The browser has no knowledge of the fact that the Servlet in the web-container is actually returning a .jar file.

Regards,
Frits
Vishal Kukreja
Greenhorn

Joined: Feb 22, 2012
Posts: 9
OK, Thanks for info Frits
jalil idrissi
Greenhorn

Joined: Feb 05, 2013
Posts: 6
try this:



OCEJWCD 6
OCPJP 7
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet Code to download Jar - HFJS