SHARE
TWEET

Untitled

a guest Jun 18th, 2017 63 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.uca.tecnoweb.servlet;
  2.  
  3. import java.io.*;
  4. import java.util.*;
  5. import java.sql.*;
  6. import javax.servlet.*;
  7. import javax.servlet.http.*;
  8. import net.sf.jasperreports.engine.*;
  9.  
  10. /**
  11.  *
  12.  * @author Byron
  13.  */
  14. public class ReportServlet extends HttpServlet {
  15.  
  16.     /**
  17.      * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
  18.      * @param request servlet request
  19.      * @param response servlet response
  20.      * @throws ServletException if a servlet-specific error occurs
  21.      * @throws IOException if an I/O error occurs
  22.      */
  23.     protected void processRequest(HttpServletRequest request, HttpServletResponse response)
  24.     throws ServletException, IOException {
  25.         // Introducir en el flujo de salida del servlet el PDF generado por el reporte.
  26.         ServletOutputStream servletOutputStream = response.getOutputStream();
  27.         InputStream reportStream = this.getServletConfig()
  28.                                        .getServletContext()
  29.                                        .getResourceAsStream("/WEB-INF/reports/listaAlumnos.jasper");
  30.         // Conectividad para BD.
  31.         String url = "jdbc:mysql://localhost:3306/academica";
  32.         String driver = "com.mysql.jdbc.Driver";
  33.         String usuario = "root";
  34.         String clave = "root";
  35.  
  36.         // Para par├ímetros al reporte.
  37.         HashMap params = new HashMap();
  38.         String carnetAlumno = "00001234";
  39.         params.put("CARNET_ALUMNO", carnetAlumno);
  40.  
  41.         try {
  42.             // Cargar driver de RDBMS y establecer conexi├│n.
  43.             Class.forName(driver);
  44.             Connection con = DriverManager.getConnection(url, usuario, clave);
  45.  
  46.             // Enviamos el Pdf del reporte a un flujo de salida, en este caso el flujo de salida del Servlet.
  47.             JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, params, con);
  48.             response.setContentType("application/pdf");
  49.  
  50.             //Cerramos la casa.
  51.             con.close();
  52.             reportStream.close();
  53.             servletOutputStream.flush();
  54.             servletOutputStream.close();
  55.         } catch (JRException ex) { //JasperReports Exception
  56.             ex.printStackTrace();
  57.         } catch (SQLException ex) {
  58.             ex.printStackTrace();
  59.         } catch (ClassNotFoundException ex) {
  60.             ex.printStackTrace();
  61.         }
  62.     }
  63.  
  64.     // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
  65.     /**
  66.      * Handles the HTTP <code>GET</code> method.
  67.      * @param request servlet request
  68.      * @param response servlet response
  69.      * @throws ServletException if a servlet-specific error occurs
  70.      * @throws IOException if an I/O error occurs
  71.      */
  72.     @Override
  73.     protected void doGet(HttpServletRequest request, HttpServletResponse response)
  74.     throws ServletException, IOException {
  75.         processRequest(request, response);
  76.     }
  77.  
  78.     /**
  79.      * Handles the HTTP <code>POST</code> method.
  80.      * @param request servlet request
  81.      * @param response servlet response
  82.      * @throws ServletException if a servlet-specific error occurs
  83.      * @throws IOException if an I/O error occurs
  84.      */
  85.     @Override
  86.     protected void doPost(HttpServletRequest request, HttpServletResponse response)
  87.     throws ServletException, IOException {
  88.         processRequest(request, response);
  89.     }
  90.  
  91.     /**
  92.      * Returns a short description of the servlet.
  93.      * @return a String containing servlet description
  94.      */
  95.     @Override
  96.     public String getServletInfo() {
  97.         return "Short description";
  98.     }// </editor-fold>
  99.  
  100. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top