0 vote against Favourite you will see I have a problem, I must from a button in a jsp, send a call to a report that I have made in jasperreport before and be displayed in a pdf window to the user.
I have tried several ways but it does not work ...
When you click on the button, you must send a signal to the servlet and servlet to decide if it shows the report to the user, then return the response to the jsp and show the user the report, but nevertheless, I can not make it show ... please help ... my code goes like this:
THIS IS THE FIRST WAY I HAVE TRIED # 1: FORM AND JSP BUTTON:
<!-- este es mi form que se conecta con el servlet Direccion1-->
<form name="FormReporte" action="Direccion1" method="POST">
<p style="color:#ffffff">
Generar Reporte de Pacientes: <br><input type="submit" name="btnreportes" value="GENERAR" class="button1"/>
<!-- este de acá es mi boton que al darle envía la informacion al servlet-->
</p>
</form>
ON THE SERVLET: I practically only validate if a String value is returned, return an answer:
//si el boton se pulsa
} else if (request.getParameter("btnreportes")!=null){
String respuesta = request.getParameter("btnreportes");//obtiene el value del boton
request.setAttribute("resreporte",respuesta);//lo coloca en resreporte
rd=request.getRequestDispatcher("medpacientes.jsp");//manda la respuesta al jsp
rd.forward(request, response); //termina la instruccion de envio
}
Returned in the jsp (where the button was):
<%
if(request.getAttribute("resreporte")!=null)//si la respuesta del servlet es diferente de vacía
{
//creo mi reporte al usuario, pero es lo que no me deja, siempre me tira error http 500 server
File reportfile = new File(application.getRealPath("RPacientes.jasper"));
Map parameter = new HashMap();
byte[] bytes = JasperRunManager.runReportToPdf(reportfile.getPath(),parameter, con);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream outputstream = response.getOutputStream();
outputstream.write(bytes,0, bytes.length);
outputstream.flush();
outputstream.close();
}
%>
WAY # 2: The jsp is the same as the way # 1, this sends to my servlet Address1
SERVLET ADDRESS 1: MORE THAN ANYTHING WHAT IT GIVES ME WHEN PRESSING THE BUTTON, IT IS THAT THE PAGE IS IN WHITE AND DOES NOT SHOW ANY ERROR
//SI MI BOTON SE PRESIONA
else if (request.getParameter("btnreportes")!=null){
Connection con = null;
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = (Connection) DriverManager.getConnection("jdbc:sqlserver://localhost\SQLEXPRESS:1433;databaseName=hospital", "sa", "12345678");
//GENERO EL REPORTE EN MI SERVLET
File reportfile = new File(request.getServletContext().getRealPath("RPacientes.jasper"));
Map parameter = new HashMap();
byte[] bytes = JasperRunManager.runReportToPdf(reportfile.getPath(), parameter, con);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream outputstream = response.getOutputStream();
outputstream.write(bytes, 0, bytes.length);
outputstream.flush();
outputstream.close();
return;//REGRESO A LA VISTA MOSTRANDO A MI USUARIO EL REPORTE
}
MY PACKAGES AND ARCHIVES ARE ORDERED IN THE FOLLOWING WAY:
However, I've already tried moving the report to the webpages folder, the driver package and everything but it does not work, help Thanks! Please ask for your help if something I am doing wrong, in advance Thank you!