I have a search criteria as shown above and also a text field. Here i am using jasper reports. So i have created the parameters in ireport-5.6.0, its working fine in internal preview of ireport-5.6.0, but when i want to send the values that is selected from a jsp page to ireport and print the data in PDF. Its not working. Only for one parameter its working and it is not taking the other two values that are selected.Here are my jsp pages.
Purchase.jsp
<form action="view.jsp" method="post">
<select name="complan">
<option value="">Make a selection</option>
<option value="Company Name">Company Name</option>
<option value="Contact Person">Contact Person</option>
<option value="Phone Number">Phone Number</option>
</select>
<select name="category">
<option value=""> Make a selection </option>
<option value="company">company</option>
<option value="institution">institution</option>
<option value="hospital">hospital</option>
<option value="Others">Others</option>
</select>
<input type="text" name="search"/>
<input type="submit" value="Submit"/>
</form>
view.jsp
<script type="text/javascript">
function setAction(nPage){
document.forms[0].action = nPage;
}
</script>
<form>
<%
String search=request.getParameter("search");
session.setAttribute("sea",search);
String category=request.getParameter("category");
session.setAttribute("cat",category);
String complan = request.getParameter("complan");
session.setAttribute("com",complan);
%>
<select onchange="setAction(this.value)">
<option value=''> Make a selection </option>
<option value='PDF_LEAD.jsp'> PDF</option>
<option value='XLS_LEAD.jsp'> XLS </option>
<option value='DOC_LEAD.jsp'> DOC </option>
<option value='XLSX_LEAD.jsp'> XLSX </option>
</select>
<br/>
<input type="submit" value="Submit">
</form>
PDF_LEAD.jsp
<body>
<%
Connection conn = null;
String sear=(String)session.getAttribute("sea");
String cate=(String)session.getAttribute("cat");
String comp=(String)session.getAttribute("com");
try
{
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/marketing_database","root","root");
String jrxmlFile ="D:/dev/tools/jasper files/report10.jrxml";
InputStream input = new FileInputStream(new File(jrxmlFile));
JasperDesign jasperDesign = JRXmlLoader.load(input);
System.out.println("Compiling Report Designs");
JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign);
System.out.println("Creating JasperPrint Object");
Map parameters = new HashMap();
parameters.put("complan",comp);
parameters.put("search",sear);
parameters.put("category",cate);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport,parameters,conn);
byte bytes[] = new byte[10000];
JRPdfExporter exporter = new JRPdfExporter();
ByteArrayOutputStream PDFStream = new ByteArrayOutputStream();
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, PDFStream);
exporter.exportReport();
System.out.println("Size of byte array:"+PDFStream.size());
bytes = PDFStream.toByteArray();
response.setContentType("application/pdf");
System.out.println("After JasperPrint = 1");
response.setContentLength(bytes.length);
System.out.println("After JasperPrint = 2");
PDFStream.close();
System.out.println("After JasperPrint = 3");
OutputStream outputStream = response.getOutputStream();
System.out.println("After JasperPrint = 4");
outputStream.write(bytes, 0, bytes.length);
outputStream.flush();
outputStream.close();
}
catch(Exception e)
{e.printStackTrace();}
%>
</body>
First i have to thank @Peter Friberg,because without you it wont be possible. I removed "default value expression" in jrxml. That is as follows.
<parameter name="category" class="java.lang.String">
<parameterDescription><![CDATA[]]></parameterDescription>
</parameter>
<parameter name="search" class="java.lang.String">
<parameterDescription><![CDATA[]]></parameterDescription>
</parameter>
<queryString>
<![CDATA[select * from lead where category ='$P!{category}' and Company_Name like '$P!{search}%']]>
</queryString>
My SQL query is
select * from lead where category ='$P!{category}' and Company_Name like '$P!{search}%'