Jump to content
Changes to the Jaspersoft community edition download ×

exception invalid stream header, streamcorrupted


sijujosethomas

Recommended Posts

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: 4A524170

java.io.StreamCorruptedException: invalid stream header: 4A524170

 

can someone help me on this please...

thanks in advance.

Link to comment
Share on other sites

  • Replies 4
  • Created
  • Last Reply

Top Posters In This Topic

  • 4 months later...

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 account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...