title: Displaying a pdf from Jasper Report using JSP?
body: <p>I am trying to display a PDF generated with Jasper Report Studio using a JSP page, but when I run the project the page doesn't load.</p>
  
  <p>here is my jsp code :</p>
  
  <pre><code>&lt;%@ page contentType="application/pdf"%&gt;
  &lt;%@ page trimDirectiveWhitespaces="true"%&gt;
  
  &lt;%@ page import="net.sf.jasperreports.engine.*"%&gt;
  &lt;%@ page import="java.io.File"%&gt;
  &lt;%@ page import="java.io.FileInputStream"%&gt;
  &lt;%@ page import="java.io.FileNotFoundException"%&gt;
  &lt;%@ page import="java.io.InputStream"%&gt;
  &lt;%@ page import="java.sql.Connection"%&gt;
  &lt;%@ page import="java.sql.DriverManager"%&gt;
  &lt;%@ page import="java.sql.SQLException"%&gt;
  
  
  &lt;%
      Connection conn = null;
      try{
          Class.forName("com.mysql.jdbc.Driver");
          conn = DriverManager.getConnection("jdbc:mysql://localhost:5222/crf", "root", "123456");
          String jrxmlFile = session.getServletContext().getRealPath("C:\\Users\\Kenji\\JaspersoftWorkspace\\MyReports\\Blank_A4.jrxml");
          InputStream input = new FileInputStream(new File(jrxmlFile));
          JasperReport jr = JasperCompileManager.compileReport(input);
          JasperPrint jp = JasperFillManager.fillReport(jr, null,conn);
          JasperExportManager.exportReportToPdfStream(jp,response.getOutputStream());
          response.getOutputStream().flush();
          response.getOutputStream().close();
          conn.close();
      }catch(Exception e){
          e.printStackTrace();
      }
  
  %&gt;
  </code></pre>
  
  <p>and here is what I get when I run the project :</p>
  
  <p><img src="https://i.stack.imgur.com/sjLkq.jpg" alt="enter image description here"></p>
  
  <p>The page is stuck on loading.</p>
  
  <p>Here is the log of the GlassFish Server :</p>
  
  <pre><code>Grave:   com.mysql.jdbc.exceptions.jdbc4.CommunicationsException: Communications link failure
  The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
      at com.mysql.jdbc.SQLError.createCommunicationsException(SQLError.java:1117)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:355)
      at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2461)
      at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2498)
      at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2283)
      at com.mysql.jdbc.ConnectionImpl.&lt;init&gt;(ConnectionImpl.java:822)
      at com.mysql.jdbc.JDBC4Connection.&lt;init&gt;(JDBC4Connection.java:47)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
      at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
      at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
      at com.mysql.jdbc.Util.handleNewInstance(Util.java:411)
      at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:404)
      at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:317)
      at java.sql.DriverManager.getConnection(DriverManager.java:664)
      at java.sql.DriverManager.getConnection(DriverManager.java:247)
      at org.apache.jsp.WEB_002dINF.jasper_005freport_jsp._jspService(jasper_005freport_jsp.java:57)
      at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:111)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
      at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:411)
      at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
      at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
      at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:875)
      at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739)
      at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:575)
      at org.apache.catalina.core.ApplicationDispatcher.doDispatch(ApplicationDispatcher.java:546)
      at org.apache.catalina.core.ApplicationDispatcher.dispatch(ApplicationDispatcher.java:428)
      at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:378)
      at Pack1.Servletta.doGet(Servletta.java:48)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
      at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
      at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318)
      at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160)
      at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734)
      at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673)
      at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99)
      at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174)
      at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:415)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:282)
      at com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459)
      at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167)
      at org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:201)
      at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:175)
      at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235)
      at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:284)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:201)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:133)
      at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
      at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77)
      at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:561)
      at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56)
      at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:565)
      at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:545)
      at java.lang.Thread.run(Thread.java:745)
  Caused by: java.net.ConnectException: Connection refused: connect
      at java.net.DualStackPlainSocketImpl.connect0(Native Method)
      at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
      at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:345)
      at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
      at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
      at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
      at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
      at java.net.Socket.connect(Socket.java:589)
      at java.net.Socket.connect(Socket.java:538)
      at java.net.Socket.&lt;init&gt;(Socket.java:434)
      at java.net.Socket.&lt;init&gt;(Socket.java:244)
      at com.mysql.jdbc.StandardSocketFactory.connect(StandardSocketFactory.java:259)
      at com.mysql.jdbc.MysqlIO.&lt;init&gt;(MysqlIO.java:305)
      ... 59 more
  </code></pre>