0

I've made (or at least tried to make) a servlet that converts a JasperPrint object to PDF and also opens this PDF in a new tab. But it seems like my code invoker is not invoking my servlet and also it's not throwing any exception.

When I call the URL directly from the browser, it does call the servlet, but the same doesn't happen from my java class.

Invoker Code:

URL url = new URL("http://localhost:8080/app/reportgenerator");
HttpURLConnection connection =  (HttpURLConnection)url.openConnection(); 

connection.setRequestMethod("POST");
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setDefaultUseCaches (false);
connection.setRequestProperty("Content-Type", "application/octet-stream");

ObjectOutputStream out = new ObjectOutputStream(connection.getOutputStream());
JasperPrint jasperPrint = new JasperPrint();
out.writeObject(jasperPrint);           
out.close();

Servlet Code:

response.setContentType("application/pdf");
response.setHeader("Content-Disposition","attachment; filename=\"report.pdf\"");

JasperPrint jasperPrint = null;

try {
    ObjectInputStream resultStream = new ObjectInputStream(request.getInputStream());
    jasperPrint = (JasperPrint) resultStream.readObject();
    resultStream.close();

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

What am I doing wrong?

Alex K
  • 22,315
  • 19
  • 108
  • 236
Diego Urenia
  • 1,620
  • 2
  • 21
  • 28
  • 1
    So what happens when you call that from java ? Do you get any error ? Can you post the stack trace ? – David Levesque Jul 04 '12 at 23:43
  • No, I wasn't getting any error. That was really weird, but i've found the solution and shared it in the answer below. Thanks for your answer anyway – Diego Urenia Jul 05 '12 at 12:14
  • possible duplicate of [Calling a Servlet from a Java application](http://stackoverflow.com/questions/4349854/calling-a-servlet-from-a-java-application) – FoamyGuy Jul 07 '12 at 03:52

1 Answers1

0

I found the solution after a lot more research. That was how I've solved my problem: Calling a Servlet from a Java application

Community
  • 1
  • 1
Diego Urenia
  • 1,620
  • 2
  • 21
  • 28