Share My Code Java to Print on the Web

package softmek;

/**
*

  • @author JuanOrtiz
    */

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.logging.Level;
import java.util.logging.Logger;

public class posconnect {

public static ResultSet DBResultSet(String shost, String database, String query) {
    try {
        String driver = "org.postgresql.Driver";
        String url = "jdbc:postgresql://"+shost+":5432/" + database;
        String username = "postgres";
        String n = "passs";
        Connection con;
        ResultSet rs;
        PreparedStatement ps;
        try {
            try {
                Class.forName(driver).newInstance();
               
            } catch (InstantiationException ex) {
                //  Logger.getLogger(posconnect.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                //  Logger.getLogger(posconnect.class.getName()).log(Level.SEVERE, null, ex);
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(posconnect.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        con = DriverManager.getConnection(url, username, n);
        //  st = con.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        ps = con.prepareStatement(query, ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
        rs = ps.executeQuery();
        con.close();
        return rs;
    } catch (SQLException ex) {
        return null;
    }

}

}

<%@page import=“net.sf.jasperreports.engine.JasperRunManager”%>
<%@page import=“net.sf.jasperreports.engine.export.JRHtmlExporterParameter”%>
<%@page import=“net.sf.jasperreports.engine.JRExporterParameter”%>
<%@page import=“net.sf.jasperreports.j2ee.servlets.ImageServlet”%>
<%@page import=“net.sf.jasperreports.engine.export.JRHtmlExporter”%>
<%@page import=“net.sf.jasperreports.j2ee.servlets.BaseHttpServlet”%>
<%@page import=“net.sf.jasperreports.engine.JRRuntimeException”%>
<%@page import=“net.sf.jasperreports.view.JasperViewer”%>
<%@page import=“java.util.HashMap”%>
<%@page import=“net.sf.jasperreports.engine.JasperPrint”%>
<%@page import=“softmek.posconnect”%>
<%@page import=“net.sf.jasperreports.engine.JasperFillManager”%>
<%@page import=“net.sf.jasperreports.engine.JRResultSetDataSource”%>
<%@page import=“java.io." %>
<%@page import="java.sql.
” %>
<%

String database = "Db";
String tableName = "invoicedt";
String Idd=request.getParameter("Idd");
String shost=request.getParameter("shost");
request.setAttribute("tableName", tableName);
//String query = "Select * from " + tableName+ " Where cntid="+Id ;
String query ="Select departamento.name as name ,sum(extprice) as sTotal, sum(citytaxv)+sum(statetaxv) as TotalTax, sum(citytaxv+statetaxv) +sum(extprice) As TotalFin , count(qty) as sQty From invoicedt INNER JOIN departamento on invoicedt.departamento=departamento.id Where cajareg=" + Idd +" and tipo='V' and grupacc<>101 GROUP BY departamento.name Order By departamento.name";
ResultSet rs = posconnect.DBResultSet(shost,database, query);
String reportFileName = application.getRealPath("/reports/VentasDep.jasper");

File reportFile = new File(reportFileName);
if (!reportFile.exists()) {
    throw new JRRuntimeException("File Autoinformation.jasper not found. The report design must be compiled first.");
}

JRResultSetDataSource resultSetDataSource = new JRResultSetDataSource(rs);
//  System.out.println("Filling report...");

// JasperPrint jasperPrint = JasperFillManager.fillReport(reportFileName,
// new HashMap(), resultSetDataSource);

byte[] bytes = JasperRunManager.runReportToPdf(reportFileName,new HashMap(), resultSetDataSource);

response.setContentType("application/pdf");
response.setContentLength(bytes.length);
ServletOutputStream ouputStream = response.getOutputStream();
ouputStream.write(bytes, 0, bytes.length);
ouputStream.flush();
ouputStream.close();

Then Call the Reports from RS

dim shost as string
shost=IpServer
MsgBox shost
Dim f As Integer
f=Val(TxtCaja.Text.Trim)

HTMLViewer1.URL="http://"+shost+":8080/CallReports/reports/VentasDep?shost="+shost+"&"+"Idd="+Str(f)

you can Print PDF with no Problems

You Need Apache to host the Reports

iReports IDE from Jasper