日期:2014-05-17 浏览次数:20971 次
项目是B/S的,标示层使用的是Openlaszlo,客户要求在打印的时候不需要选择打印的纸张类型,报表显示就直接打印,所以我在客户做了一个小应用程序。
需要的jar有:msbase.jar;mssqlserver.jar;msutil.jar;jasperreports-2.0.5-applet.jar;
jasperreports-1.3.3.jar;jasperreports-2.0.5-javaflow.jar
需要使用Ireport设计模板,放在reports目录下,载入*.jasper文件
<%@ page contentType="text/html;charset=UTF-8"%> <%@page import="java.util.Enumeration"%> <%@page import="java.net.URLDecoder"%> <% String sql = request.getParameter("sql"); sql="select * from authors"; System.out.println("viewer.jsp =========:"+sql); // System.out.println("viewer.jsp =========:"+new String(condition.getBytes("ISO8859_1"),"UTF-8")); %> <html> <head> </head> <body> <!--"CONVERTED_APPLET"--> <!-- HTML CONVERTER --> <object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" codebase="http://java.sun.com/update/1.5.0/jinstall-1_5-windows-i586.cab#Version=5,0,0,5" WIDTH="100%" HEIGHT="100%"> <PARAM NAME=CODE VALUE="ViewerApplet.class"> <param name="scriptable" value="false"> <PARAM NAME=CODEBASE VALUE="applet"> <PARAM NAME=ARCHIVE VALUE="jasperreports-2.0.5-javaflow.jar,jasperreports-2.0.5-applet.jar,commons-collections-3.1.jar"> <param name="type" value="application/x-java-applet;version=1.5"> <PARAM NAME="sql" VALUE="<%=sql%>"> <comment> <embed type="application/x-java-applet;version=1.5" \ ARCHIVE="jasperreports-2.0.5-javaflow.jar,jasperreports-2.0.5-applet.jar,commons-collections-3.1.jar" \ WIDTH="100%" \ HEIGHT="100%" \ CODE="ViewerApplet.class" \ CODEBASE="applet" \ ARCHIVE="jasperreports-2.0.5-javaflow.jar,jasperreports-2.0.5-applet.jar,commons-collections-3.1.jar" \ scriptable="false" \ sql="<%=sql%>" \ scriptable=false pluginspage="http://java.sun.com/products/plugin/index.html#download"> <noembed> </XMP> </noembed> </embed> </comment> </object> </body> </html>
?做一个ViewerApplet的类,继承JApplet
import java.awt.BorderLayout; import java.io.PrintWriter; import java.io.StringWriter; import java.net.URL; import java.net.URLEncoder; import javax.swing.JApplet; import javax.swing.JOptionPane; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.util.JRLoader; import net.sf.jasperreports.view.JRViewer; public class ViewerApplet extends JApplet { public ViewerApplet() { initComponents(); } private javax.swing.JPanel pnlMain; public void init() { try { //获取viewer.jsp的sql参数 String sql = getParameter("sql"); //使用URLEncoder对sql语句进行encode sql = URLEncoder.encode(sql, "UTF-8"); //提交到servlet,使用servlet访问服务器端数据 URL url = new URL(getCodeBase(), "../servlet/appletServlet?sql=" + sql); if (url != null) { //获取服务器的传递过来的JasperPrint对象 JasperPrint jasperPrint = (JasperPrint) JRLoader .loadObject(url); //创建一个JRViewer JRViewer viewer = new JRViewer(jasperPrint); this.pnlMain.add(viewer, BorderLayout.CENTER); } } catch (Exception e) { StringWriter swriter = new StringWriter(); PrintWriter pwriter = new PrintWriter(swriter); e.printStackTrace(pwriter); JOptionPane.showMessageDialog(this, swriter.toString()); } } private void initComponents() {// GEN-BEGIN:initComponents pnlMain = new javax.swing.JPanel(); pnlMain.setLayout(new java.awt.BorderLayout()); getContentPane().add(pnlMain, java.awt.BorderLayout.CENTER); } }
?创建一个AppletServlet类
package com.gddzmr.servlet; import java.io.IOException; import java.io.ObjectOutputStream; import javax.servlet.ServletException; impor