0

I am going through a confusing situation where when button is clicked a pdf should get downloaded. My design and requirement is something like below:

  1. Click the button
  2. show confirm box with two buttons save and open
  3. If save is selected then pdf should be saved on local computer.

3rd point is where I am facing the problem because when there is no confirm box. Here when form is submitted using submit button (no confirm box) then file is getting downloaded. Below is code:

<button type="submit" id="Export">xxx_tutorial</button>

But when there is just a button with onclick event for confirmation box written in jQuery and I have used $Post to submit the data to servlet where data is passed to servlet but file is not getting downloaded.

Now my question is:

  1. Is passing the data through $post is not equivalent to submit along with passing data to servlet what happens when submit button is clicked.

  2. When the data is passed to servlet why file is not getting downloaded where the same code is working when using submit button without confirmation box.

Below is code:

JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.11.2/themes/redmond/jquery-ui.css">
<script src="https://code.jquery.com/jquery-1.11.2.js"></script>
<script src="https://code.jquery.com/ui/1.11.2/jquery-ui.js"></script>
<!-- User Defined Js file -->
<script type="text/javascript">

$(document).ready(function(){
          $('#Export').click(function(event) {  
          event.preventDefault();
        var currentForm = $(this).closest('form');

        var dynamicDialog = $('<div id="conformBox">'+
        '<span style="float:left; margin:0 7px 20px 0;">'+
        '</span>Open or save the document</div>');

        dynamicDialog.dialog({
                title : "Open/Save Dialog",
                closeOnEscape: true,
                modal : true,

               buttons : 
                        [{
                                text : "Export",
                                click : function() {

                                    $(this).dialog("close");
                                    var data = "xxx_tutorial";
                                    $.post('button', {param: data}, function(param){


            });
                                }
                        },
                        {
                                text : "Open",
                                click : function() {
                                        $(this).dialog("close");
                                }
                        }]
        });
        return false;
    });

}); 
</script>
</head>
<body>
        <button type="button" id="Export">xxx_tutorial</button>

</body>
</html>

Servlet:

package com.testcase.testing;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class button
 */
@WebServlet("/button")
public class button extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public button() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doPost(request, response);
        }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub

        response.getWriter().append(request.getParameter("param"));
        performTask(request,response);
    }

    private void performTask(HttpServletRequest request, HttpServletResponse response) throws ServletException,
    IOException {

        String pdfFileName="";

        if(request.getParameter("param")==null){

    }
    else if(request.getParameter("param").matches("sap_webi_tutorial")){
        System.out.println("in create pdf file name part");
        pdfFileName = "/"+request.getParameter("param")+".pdf";
    }
        else{

        }

String contextPath = getServletContext().getRealPath(File.separator);
File pdfFile = new File(contextPath + "/xxx_tutorial.pdf"); 
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment; filename=" + "/xxx_tutorial.pdf");
response.setContentLength((int) pdfFile.length());

FileInputStream fileInputStream = new FileInputStream(pdfFile);
PrintWriter responseOutputStream = response.getWriter();
int bytes;
    while ((bytes = fileInputStream.read()) != -1) {
    responseOutputStream.write(bytes);
}
fileInputStream.close();
responseOutputStream.flush();
responseOutputStream.close();
    }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
Siva
  • 9,043
  • 12
  • 40
  • 63

1 Answers1

0

If you submit the form, the page displayed in the browser will receive the reults of that operation - it's like clicking a link so to say.

Calling $.post() would instead send data in the background and return the result-data into your callback-function (which is empty in the code you posted). So you'd have to deal with the result in that function.

See post documentation for background

Maybe you could do your logic and then instead of calling $.post() you simply ubmit the form from within JavaScript code?

Check this for submitting your form directly:

JavaScript post request like a form submit

Jan
  • 13,738
  • 3
  • 30
  • 55
  • Sir thanks for the answer... actually my code for download is in servlet so I have just passed the data to servlet but how do I catch the download file back in jsp is there any way for this, because I tried with javascript but my requirement is I need to have a custom confirmation box which is possible only through Jquery – Siva May 27 '17 at 09:32