sijujosethomas Posted March 15, 2007 Share Posted March 15, 2007 i'm trying to pass the JasperPrint object from the servlet to the applet and view the report through the applet my code is as follows: my servlet: import java.io.IOException;import java.io.ObjectOutputStream;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.util.HashMap; import javax.servlet.ServletException;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse; import net.sf.jasperreports.engine.JRException;import net.sf.jasperreports.engine.JRResultSetDataSource;import net.sf.jasperreports.engine.JasperCompileManager;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; public class JRServlet extends HttpServlet { private Connection con = null; private PreparedStatement pst = null; private ResultSet rs = null; protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doPost(req,res); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { try {DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());con = DriverManager.getConnection("mydb", "username","password");System.out.println("connection created");String sql = "my query";pst = con.prepareStatement(sql); rs = pst.executeQuery(); JRResultSetDataSource jrds = new JRResultSetDataSource(rs); JasperDesign jd = JRXmlLoader.load("myreportfile.jrxml");JasperReport jr = JasperCompileManager.compileReport(jd); JasperPrint jp = JasperFillManager.fillReport(jr, new HashMap(), jrds); res.setContentType("application/octet-stream"); ServletOutputStream outstr = res.getOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(outstr); oos.writeObject(jp); oos.flush(); oos.close(); outstr.flush(); outstr.close(); } catch (SQLException se) { System.out.println("SQLException:" + se.getMessage()); } catch (JRException jre) { System.out.println("JRException:" + jre.getMessage()); } finally { try { rs.close(); pst.close(); con.close(); } catch (SQLException se) { } } } } my applet: import java.applet.Applet;import java.awt.Button;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection; import net.sf.jasperreports.engine.JasperPrint;import net.sf.jasperreports.view.JasperViewer; public class JRApplet extends Applet { private Button openJRViewer = new Button("Open"); public void init() { add(openJRViewer); openJRViewer.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { receiveData(); } }); }private URLConnection getServletConnection() throws MalformedURLException, IOException { URL urlServlet = new URL(getCodeBase(), "servlet"); URLConnection con = urlServlet.openConnection(); con.setDoInput(true); con.setDoOutput(false); con.setUseCaches(false); con.setRequestProperty("Content-Type", "application/x-java-serialized-object"); return con; }private void receiveData() { try { System.out.println("inside method"); //connection from servlet URLConnection con = getServletConnection(); System.out.println("after conn"); // reveive data from servlet InputStream instr = con.getInputStream(); System.out.println("after instr "); ObjectInputStream inputFromServlet = new ObjectInputStream(instr); System.out.println("after ois"); JasperPrint jpo = (JasperPrint) inputFromServlet.readObject(); System.out.println("after jpo"); JasperViewer.viewReport(jpo); System.out.println("after view"); } catch (MalformedURLException e) { System.out.println("MalformedURLException:" + e.getMessage()); } catch (IOException ie) { System.out.println("IOException:" + ie.getMessage()); ie.printStackTrace(); } catch (ClassNotFoundException ce) { System.out.println("ClassNotFoundException:" + ce.getMessage()); } } } but i'm getting the following error: IOException:invalid stream header: 4A524170java.io.StreamCorruptedException: invalid stream header: 4A524170 can someone help me on this please...thanks in advance. Link to comment Share on other sites More sharing options...
sijujosethomas Posted March 15, 2007 Author Share Posted March 15, 2007 can we send the JasperPrint object from the servlet to an applet through the response?? Link to comment Share on other sites More sharing options...
Eng_Faris_Zuriekat Posted July 27, 2007 Share Posted July 27, 2007 I had similar problems with passing sub reports. I think you may need to pass the the binary code and re-execute it, since the " invalid stream header" refers to a problem parsing the passed file ".jprint". Link to comment Share on other sites More sharing options...
martynhiemstra Posted July 27, 2007 Share Posted July 27, 2007 Load your reports by putting them in your classpath. Then load them with your ClassLoader. Then create a jasperprint and then let the JRViewer display the report. Then there is no need for a outputstream what so ever, JRViewer will just appear in front of the applet. Link to comment Share on other sites More sharing options...
Teodor Danciu Posted July 30, 2007 Share Posted July 30, 2007 Hi, The /demo/samples/webapp sample provided with the project shows you how to use the JasperReports Swing viewer to show reports sent in JasperPrint format by a servlet. I hope this helps.Teodor Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now