Skip to main content
added 253 characters in body
Source Link

I amWhen you click on the link, the ajax code will not sure what yourbe called so the filename parameter will not be included in the request to the servlet do but. The servlet will execute with filename = null. That is the actual result you can lookgot.

To fix this, I think you have to call the ajax code at these thingsthe first time the page loaded so that your servlet can create a file and place it on the server. Then you have to pass filename parameter in your link such as:

  1. Did you declare your servlet and the servlet-mapping (/export.zip) in web.xml?

  2. Try this for passing parameter in your ajax call:

     $.ajax({
         type: 'post',
         url: '/export.zip',
         data: {filename : 'myFile'},
         success:function(data){alert(data);},
         error:function(){alert('error');}
     });
    
<a href="http://yourdomain.com/downloadFile?filename=myFile">Download file</a>

The downloadFile servlet will look for file named myFile.txt which was created at the first time your page loaded with ajax called, and give you the file in response.

I am not sure what your servlet do but you can look at these things:

  1. Did you declare your servlet and the servlet-mapping (/export.zip) in web.xml?

  2. Try this for passing parameter in your ajax call:

     $.ajax({
         type: 'post',
         url: '/export.zip',
         data: {filename : 'myFile'},
         success:function(data){alert(data);},
         error:function(){alert('error');}
     });
    

When you click on the link, the ajax code will not be called so the filename parameter will not be included in the request to the servlet. The servlet will execute with filename = null. That is the actual result you got.

To fix this, I think you have to call the ajax code at the first time the page loaded so that your servlet can create a file and place it on the server. Then you have to pass filename parameter in your link such as:

<a href="http://yourdomain.com/downloadFile?filename=myFile">Download file</a>

The downloadFile servlet will look for file named myFile.txt which was created at the first time your page loaded with ajax called, and give you the file in response.

deleted 30 characters in body
Source Link
Luiggi Mendoza
  • 85.9k
  • 16
  • 159
  • 354

I am not sure what your servlet do but you can look at these things:

  1. Did you declare your servlet and the servlet-mapping (/export.zip) in web.xml?

    Did you declare your servlet and the servlet-mapping (/export.zip) in web.xml?

  2. Try this for passing parameter in your ajax call:

    Try this for passing parameter in your ajax call:

     $.ajax({
         type: 'post',
         url: '/export.zip',
         data: {filename : 'myFile'},
         success:function(data){alert(data);},
         error:function(){alert('error');}
     });
    
    $.ajax({
        type: 'post',
        url: '/export.zip',
        data: {filename : 'myFile'},
        success:function(data){alert(data);},
        error:function(){alert('error');}
    });

I am not sure what your servlet do but you can look at these things:

  1. Did you declare your servlet and the servlet-mapping (/export.zip) in web.xml?
  2. Try this for passing parameter in your ajax call:
    $.ajax({
        type: 'post',
        url: '/export.zip',
        data: {filename : 'myFile'},
        success:function(data){alert(data);},
        error:function(){alert('error');}
    });

I am not sure what your servlet do but you can look at these things:

  1. Did you declare your servlet and the servlet-mapping (/export.zip) in web.xml?

  2. Try this for passing parameter in your ajax call:

     $.ajax({
         type: 'post',
         url: '/export.zip',
         data: {filename : 'myFile'},
         success:function(data){alert(data);},
         error:function(){alert('error');}
     });
    
Source Link

I am not sure what your servlet do but you can look at these things:

  1. Did you declare your servlet and the servlet-mapping (/export.zip) in web.xml?
  2. Try this for passing parameter in your ajax call:
    $.ajax({
        type: 'post',
        url: '/export.zip',
        data: {filename : 'myFile'},
        success:function(data){alert(data);},
        error:function(){alert('error');}
    });