Skip to Main Content

Java Development Tools

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

My servlet code view the report in JasperViewer Plus download it as pdf in chrome.How to amend this

TAJDINAug 8 2015 — edited Aug 21 2015

Sir,

My servlet code done two things

1-It show the report in JasperViewer

2-It download it as pdf in chrome.

How to amend this code that it only give the view in JasperViewr?

My code is

package esh.view;

import esh.model.AppModuleImpl;

import java.io.IOException;

import java.io.InputStream;

import java.io.PrintWriter;

import java.sql.Connection;

import java.sql.PreparedStatement;

import java.sql.SQLException;

import java.util.HashMap;

import java.util.Locale;

import java.util.Map;

import javax.servlet.ServletConfig;

import javax.servlet.ServletException;

import javax.servlet.annotation.WebServlet;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JasperCompileManager;

import net.sf.jasperreports.engine.JasperExportManager;

import net.sf.jasperreports.engine.JasperFillManager;

import net.sf.jasperreports.engine.JasperPrint;

import net.sf.jasperreports.engine.JasperReport;

import net.sf.jasperreports.engine.design.JasperDesign;

import net.sf.jasperreports.engine.xml.JRXmlLoader;

import net.sf.jasperreports.view.JasperViewer;

import oracle.jbo.client.Configuration;

@WebServlet(name = "MultiReportServletJasper", urlPatterns = { "/multireportservletjasper" })

public class MultiReportServletJasper extends HttpServlet {

    private static final String CONTENT_TYPE = "text/html; charset=UTF-8";

    public void init(ServletConfig config) throws ServletException {

        super.init(config);

    }

    protected Connection getConnection() {

           

            PreparedStatement st = null;

            String amDef = "esh.model.AppModule";

            String config = "AppModuleLocal";

            AppModuleImpl am = (AppModuleImpl ) Configuration.createRootApplicationModule(amDef, config);

           

            st = am.getDBTransaction().createPreparedStatement("select 1 from dual", 0);

            Connection conn = null;   

       

            try {

                conn = st.getConnection();

                return conn;

            } catch (SQLException e) {

            }  

       

            return null;

        }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String var0 = ""; 

        String reportName = ""; 

        try { 

             reportName = request.getParameter("reportName"); 

            } catch (Exception e) { 

                    e.printStackTrace(); 

                   } 

             response.setContentType(CONTENT_TYPE); 

             Map parameters = new HashMap(); 

             parameters.put("format", "pdf"); 

             parameters.put("WEBDIR", getServletContext().getRealPath("/")); 

             parameters.put("REPORT_LOCALE", new Locale("ar"));                            

                                                                                       

             Connection conn = null; 

             InputStream is = null; 

        try 

            { 

             conn = getConnection(); 

             is = getServletContext().getResourceAsStream("/WEB-INF/reports/"+reportName+".jrxml"); 

                         

                        

             response.setContentType("application/pdf"); 

             response.addHeader("Content-Disposition", "attachment; filename="+reportName+".pdf");                                       

             JasperDesign jasperDesign = JRXmlLoader.load(is); 

             JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); 

             JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, conn);                     

             JasperExportManager.exportReportToPdfStream(jasperPrint, response.getOutputStream()); 

             JasperViewer.viewReport(jasperPrint,false);                                                                               

              conn.close(); 

            } 

               catch (Exception e) { 

                     e.printStackTrace(); 

                } finally { 

                    closeConnection(conn); 

                try { 

                       if(is !=null){ 

                          is.close(); 

                     }                                                            

            } catch (Exception localException1) 

                { 

                  } 

            }                     

    }

   

    protected static void closeConnection(Connection conn) {

            try {

                if(conn !=null){

                   conn.close();

                }          

            } catch (Exception ex) {

                //  System.out.println("Developer Msg : Exception in printReport1Servlet.closeConnection()");

            }

        }

   

   

}

Regards

This post has been answered by kdario on Aug 19 2015
Jump to Answer
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 18 2015
Added on Aug 8 2015
8 comments
1,236 views