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:
- Click the button
- show confirm box with two buttons
save
andopen
- 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:
Is passing the data through
$post
is not equivalent tosubmit
along with passing data to servlet what happens when submit button is clicked.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();
}
}