views:

392

answers:

2

Hi All,

I am developing a web-app where I want to provide a download link to users - so they can download a zip file from my server. Now, my requirement is that I want to execute some processing logic contained in a servlet before displaying the file dialog to the user for 'zip' files.

So If I write

<a href="abc.zip".......>

then it opens a file dialog asking the user to select the location where this file can be saved

But if I want a servlet's doGet method to do some preprocessing - say like building the zip file , then how can I first call the servlet and then open the file dialog.

Will the following snippet work?

 <a href="MyHandlerServlet;abc.zip".......>

Thanks for your help !!

+1  A: 
 <a href="MyHandlerServlet?file=abc.zip">

The servlet would need to respond to the request with the zip file.

David Dorward
Hi David,When I tried this, it does not open the file dialog and enters the doGet method of the servlet..How do I show the file dialog to the user to support 'save as' operation?
deepthinker121
@deepthinker121 Your doGet method should return an HTTP response to the client, containing the resulting zip file. This will automatically trigger the user's browser to show the file save dialog.
Daan
@DaanThanks a lot.. :) this worked for me - I returned the file as part of the HTTP response..
deepthinker121
+1  A: 

I would rather pass the filename as pathinfo instead of request parameter, e.g.

<a href="MyHandlerServlet/abc.zip">

Otherwise a certain widely used webbrowser developed by a team in Redmond (cough) would use MyHandlerServlet as filename during Save As. When passing the filename as pathinfo, you can obtain the requested file in the servlet by:

String filename = request.getPathInfo();

You only need to map the servlet on /MyHandlerServlet/* instead of /MyHandlerServlet. Also see this basic servlet example.

BalusC
thanks BalusC for the suggestion... now works fine :)
deepthinker121