Im using Eclipse below are part of the code
<!--menu.jsp-->
<h1>Pass parameter to jasper report(CI_ID)</h1>
<form action = "call.jsp" target="_blank">
<input type= "text" name = "CI_ID" value = ""/><br/>
<input type="submit" value="Print"/>
</form>
<!--call.jsp-->
<%Connection conn = null;
int id=Integer.parseInt(request.getParameter("CI_ID"));
try{
Class.forName("org.gjt.mm.mysql.Driver").newInstance();
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","1234");
File reportFile = new File(application.getRealPath("//Report.jasper"));
Map parameters = new HashMap();
parameters.put("CI_ID",id);
byte[] bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),parameters,conn);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream outStream = response.getOutputStream();
outStream.write(bytes, 0 , bytes.length);
response.getOutputStream().flush();
response.getOutputStream().close();
} catch (Exception e){
e.printStackTrace();
}%>
I preview on the Jasper report without any problem but when I use the JSP to pass the parameter to Jasper report on Eclipse and i get this error “File does not begin with '%PDF-'.” pop up window for adobe reader.
I tried below solution, but still not working
1)Change to
response.setContentType("application/pdf");
response.setContentType("application/x-pdf");
2)Update adobe reader
3)Import all related .jar for jasper
First thing is you have session attributes and request.getParameters so that you can send those parameters to the jasper report.I will show an example for better understanding. I have a jsp page such as searchData.jsp
<form action="searchResult.jsp" method="POST">
<table border="0" width="" cellspacing="15" cellpadding="1">
<thead>
<tr>
<th>Search Using:</th>
<th><select name="search">
<option value=""> Select</option>
<option value="Company Name">Company Name</option>
<option value="Contact Person">Contact Person</option>
<option value="Phone">Phone Number</option>
</select></th>
<th><input type="text" name="search_tf" /></th>
<th><input type="submit" value=" " class="search_btn"/></th>
</tr>
</thead>
<tr>
<td></td><td></td><td></td><td></td>
<td><small>(Blank Search will result in Full Search Result)</small></td>
</tr>
</table>
</form>
After clicking submit button as i have action as "searchResult.jsp" it will go to searchResult.jsp
In searchResult.jsp page i used request.getParameter
so that the data entered in the previous jsp page will now enter into current jsp page and then i used session attributes so as it can used in the further jsp pages until session gets ended.It is as shown below
Search Result for <u><%= request.getParameter("search_tf")%></u> in <u><%= request.getParameter("search")%></u> is :
Object o2 = session.getAttribute("email");
String email = o2.toString();
String category = request.getParameter("category");
String search = request.getParameter("search");
session.setAttribute("search",search);
String search_tf = request.getParameter("search_tf");
session.setAttribute("search_tf",search_tf);
and then my sql query,i am giving an example of sql query
if(search.equals("")&& search_tf.equals("")){
sqlPgintn="SELECT SQL_CALC_FOUND_ROWS Id,Company_name,Contact_Person,Address,Phone,Company_Email,Review,Status,Lead_Date,Lead_Details,Lead_Value,followup_Date,Category from marketing_database.lead limit "+iPagNo+","+iSwRws+"";
}
else if(search.equals("Company Name"))
{
sqlPgintn="SELECT SQL_CALC_FOUND_ROWS Id,Company_name,Contact_Person,Address,Phone,Company_Email,Review,Status,Lead_Date,Lead_Details,Lead_Value,followup_Date,Category from marketing_database.lead where Company_Name ='"+search_tf+"' limit "+iPagNo+","+iSwRws+"";
And now using javascript i did this
<script type="text/javascript">
function generateReport() {
var e = document.getElementById("idOfYourSelectYouNeedToAddedIt");
var strPage = e.options[e.selectedIndex].value;
window.open(strPage);
return false; //This make you stay on this page;
//return true; //Set the action tag in the form to the page you like to go to!
}
</script>
<center> <form name="myForm" onsubmit="return generateReport()">
<select id = "idOfYourSelectYouNeedToAddedIt">
<option value=''> Generate Report </option>
<option value='samplePDF.jsp'> PDF</option>
<option value='sampleDOC.jsp'> DOC </option>
<option value='sampleXLS.jsp'> XLS</option>
<option value='sampleXLSX.jsp'> XLSX </option>
</select>
<br/>
<input type="submit" value="Submit">
</form></center>
when i click on suppose "samplePDF.jsp" it will go that page and there i did this samplePDF.jsp
<%
Connection conn = null;
Object o2 = session.getAttribute("email");
String email = o2.toString();
String category=(String)session.getAttribute("category");
String status=(String)session.getAttribute("status");
String startDate=(String)session.getAttribute("startDate");
String endDate=(String)session.getAttribute("endDate");
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/marketing_database","root","root");
String sql = "select * from lead where Email = '" + email + "' AND Status LIKE '%" + status + "%' AND Category LIKE '%" + category + "%' AND STR_TO_DATE(`Lead_Date`, '%d-%m-%Y') BETWEEN '"+startDate+"' AND '"+endDate+"'";
ServletContext context = request.getServletContext();
String fullPath = context.getRealPath("/WEB-INF/reports/report10.jrxml");
InputStream input = new FileInputStream(new File(fullPath));
JasperDesign jasperDesign = JRXmlLoader.load(input);
System.out.println("Compiling Report Designs");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
System.out.println("Creating JasperPrint Object");
HashMap<String,Object> map = new HashMap<String,Object>();
map.put("sql",sql);
byte[] bytes = JasperRunManager.runReportToPdf(jasperReport, map, conn);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream outStream = response.getOutputStream();
outStream.write(bytes, 0, bytes.length);
outStream.flush();
outStream.close();
conn.close();
}
catch(Exception e)
{e.printStackTrace();}
%>