JasperReports -- 客户端打印(WEB网页打印)

[报表]JasperReports -- 客户端打印(WEB网页打印)

JasperReports针对报表打印实际是采用applet实现的,和传统的ActiveX和ocx不太一样,需要客户机装有jre。
他是通过applet接收HttpServlet输出的打印对象实现的
这里以JasperReports -- 入门应用的编译文件作为数据源。
1我们需要自己创建一个Servlet
JaspWebPrint
import java.io.IOException;
import java.io.ObjectOutputStream;
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.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;


public class JaspWebPrint extends HttpServlet {

	private static final String CONTENTTYPE = "application/octet-stream";
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		JasperPrint jasperPrint = null;
		response.setContentType(CONTENTTYPE);   
		ServletOutputStream ouputStream = response.getOutputStream();

		try {
			jasperPrint = JasperFillManager.fillReport("demo.jasper", new HashMap(),new  JREmptyDataSource());
			ObjectOutputStream oos = new ObjectOutputStream(ouputStream);
			oos.writeObject(jasperPrint);   
			oos.flush();   
			oos.close();   
		} catch (JRException e) {
			e.printStackTrace();
		}
		
	}

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

		doGet(request,response);
	}
}

2我们需要创建一个applet,用于实现打印功能
import java.applet.Applet;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.JOptionPane;

import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperPrintManager;
import net.sf.jasperreports.engine.util.JRLoader;

public class DemoApplet extends Applet {
    private URL url;
    private JasperPrint jasperPrint;
    public DemoApplet()
    {
        url = null;
        jasperPrint = null;
    }

    public void init()
    {
    	String strUrl = getParameter("REPORT_URL");
        if(strUrl != null)
            try
            {
                url = new URL(getCodeBase(), strUrl);
            }
            catch(Exception e)
            {
                StringWriter swriter = new StringWriter();
                PrintWriter pwriter = new PrintWriter(swriter);
                e.printStackTrace(pwriter);
                JOptionPane.showMessageDialog(this, swriter.toString());
            }
        else
            JOptionPane.showMessageDialog(this, "Source URL not specified");
    }

    public void start()
    {
       if(url != null)
            try
            {
                if(jasperPrint == null)
                    jasperPrint = (JasperPrint)JRLoader.loadObject(url);
                if(jasperPrint != null)
                    JasperPrintManager.printReport(jasperPrint, false);
                else
                    JOptionPane.showMessageDialog(this, "Source URL not specified");
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
        else
            JOptionPane.showMessageDialog(this, "Source URL not specified");
    }
}

3.下面我们编写页面代码,当页面加载完毕之后自动打印。
<APPLET ID="Print" name = "app" CODE = "DemoApplet.class"  ARCHIVE = "jasperreports-3.5.0-applet.jar,jcommon-1.0.15.jar,commons-logging-1.0.2.jar,commons-collections-2.1.jar" WIDTH = "0" HEIGHT = "0" MAYSCRIPT>
	<PARAM NAME = "type" VALUE="application/x-java-applet;version=1.2.2"/>
	<PARAM NAME = "scriptable" VALUE="false"/>
	<PARAM NAME = "REPORT_URL" VALUE ="http://localhost:8080/jasport/servlet/JaspWebPrint"/>
	<PARAM NAME = "SHOW_DIALOG" VALUE ="true">
</APPLET>

这里需要注意各版本引入的jar包并不相同,这里用到的jar需要客户端可以访问到的,如有问题可以详见applet的相关开发说明。

对于经常出现Applet的警告信息的处理。
找到jre的目录 修改jre6\lib\security\java.pclicy文件内容
在grant {

};里面追加如下内容即可。
       permission    java.util.PropertyPermission    "user.dir",    "read";  
       permission    java.util.PropertyPermission    "user.home",    "read";   
       permission    java.util.PropertyPermission    "java.home",    "read";   
       permission    java.util.PropertyPermission    "java.class.path",    "read";   
       permission    java.util.PropertyPermission    "user.name",    "read";   
       permission    java.lang.RuntimePermission    "accessClassInPackage.sun.misc";   
       permission    java.lang.RuntimePermission    "accessClassInPackage.sun.audio";   
       permission    java.lang.RuntimePermission    "modifyThread";   
       permission    java.lang.RuntimePermission    "modifyThreadGroup";   
       permission    java.lang.RuntimePermission    "loadLibrary.*";   
       permission    java.io.FilePermission    "<<ALL    FILES>>",    "read";   
       permission    java.io.FilePermission    "${user.dir}${/}jmf.log",    "write";   
       permission    java.io.FilePermission    "${user.home}${/}.JMStudioCfg",    "write";   
       permission    java.net.SocketPermission    "*",    "connect,accept";   
       permission    java.io.FilePermission    "C:\WINNT\TEMP\*",    "write";   
       permission    java.io.FilePermission    "C:\WINNT\TEMP\*",    "delete";   
       permission    java.awt.AWTPermission    "showWindowWithoutWarningBanner";   
       permission    javax.sound.sampled.AudioPermission    "record";   
       permission    java.net.SocketPermission    "-",    "listen";   
       permission    java.net.SocketPermission    "-",    "accept";   
       permission    java.net.SocketPermission    "-",    "connect";   
       permission    java.net.SocketPermission    "-",    "resolve";   
       permission    java.security.AllPermission;  
       permission java.lang.RuntimePermission "accessClassInPackage.sun.awt";
       permission java.lang.RuntimePermission "accessClassInPackage.sun.awt.windows";


你可能感兴趣的:(java,Web,.net,servlet,sun)