El codigo desde mi index. jsp es:
Código jsp:
Ver original<%--
Document : index
Created on : Dec 13, 2010, 3:18:13 PM
Author : ventas
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%-- Inserto las librerias --%>
<%@page import = "java.util.*" %>
<%! String msg = "Esta es un frase de un variable";%>
<%-----------------------------------------------------------%>
<%-- Aquí comienza la pagina HTML --%>
<%-----------------------------------------------------------%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<%-- Le coloco el título a la página --%>
<title>Mi página de JSP</title>
</head>
<body>
<h1>Hello World!</h1>
<br>
<%-----------------------------------------------------------%>
<%-- Fase de captura de datos --%>
<%-----------------------------------------------------------%>
<%
/*Le digo en que formato quiero los datos*/
request.setCharacterEncoding("UTF-8");
/*Capturo el parámetro enviado*/
String a = request.getParameter("id");
/*Le asigno el valor a al variable global*/
msg = a;
%>
<%= msg%>
<%-----------------------------------------------------------%>
<%-----------------------------------------------------------%>
<%-- Link de descargar del archivo --%>
<p>Para descargar el archivo haga clic
<a href="http://localhost:8084/miWeb/servletReporte2?id=<%= msg %>" >aquí</a>
</p>
<%-- Imprimo la fecha --%>
<% out.println(""+ new Date());%>
</body>
</html>
y en mi servlet tengo:
Código java:
Ver originalpackage pkgreporte;
import java.io.File;
import java.util.HashMap;
import javax.servlet.ServletOutputStream;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperRunManager;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author MRARRB87
*/
public class servletReporte extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
response.setHeader("Cache-Control","no-cache"); //Para evitar el cache
response.setHeader("Pragma","no-cache");
response.setDateHeader ("Expires", 0);
response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment; filename=\"reporteServlet.pdf\";");
try {
Class.forName("com.mysql.jdbc.Driver"); //se carga el driver
conn
= DriverManager.
getConnection("jdbc:mysql://localhost:3306/bd_reporte_servlet",
"root",
"root"); }
ex.printStackTrace();
}
ServletOutputStream servletOutputStream = response.getOutputStream();
File reportFile
= new File(getServletConfig
().
getServletContext().
getRealPath("servlet_reporte.jasper"));
int id_tipoperosona=1;
parameters.put("Pid_tipo_persona",id_tipoperosona); //envio de parametros al reporte
byte[] bytes = null;
try {
bytes = JasperRunManager.runReportToPdf(reportFile.getPath(),parameters, conn);
response.setContentType("application/pdf");
response.setContentLength(bytes.length);
servletOutputStream.write(bytes, 0, bytes.length);
servletOutputStream.flush();
servletOutputStream.close();
} catch (JRException e){
// display stack trace in the browser
e.printStackTrace(printWriter);
response.setContentType("text/plain");
response.getOutputStream().print(stringWriter.toString());
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo
() { return "Short description";
}// </editor-fold>
}