jasperreport/ireport5.5 applet/flash jsp servlet 报表与web结合问题 菜鸟请大牛们指点

RisonGao 2014-03-09 06:04:26
一、报表方面的需求:
1、报表只能预览,不能下载或者打印;
2、报告能预览或者打印,不能下载;
3、报表可以下载到本地。
二、已经实现功能:
1、报表内容录入
2、报表内容审核
3、多页报表拼接
4、报表可以实现PDF文件预览,但是可以被下载与打印。于是想采用applet或者flash进行处理
三、技术难点(新手无力啊)
1、参数传递(例如输入报告编号为20140308,即可显示出该报告),进行拼接报表,并在applet中显示
2、参数传递(例如输入报告编号为20140308,即可显示出该报告),进行拼接报表,并在flash中显示
四、flash显示部分 表问下面flash怎么写成flasth,别人发给我就错了,我直接复制测试的。。。
1、输入报告编号页面 flashinput.jsp
 <form action="servlets/flasthservlet" method="post">
input reportid
<input type="text" name="reportid">
<input type="submit" value="submit">
</form>

2、jasperreport flash显示页面 rflash.jsp
<object height="1000" width="100%">
<param name="movie"
value="<%=request.getContextPath()%>/flash/jasperreports-flash-4.5.0.swf" />
<embed
src="<%=request.getContextPath()%>/flash/jasperreports-flash-4.5.0.swf"
FlashVars="jrpxml=<%=request.getContextPath()%>/servlets/xml4swf?&fetchsize=3"
width="100%" height="100%">
</embed>
</object>

3、web.xml 由于字数限制,删除部分servlet配置

<web-app>
<display-name>JasperReports</display-name>

<!-- 自定义Servlet -->
<servlet>
<servlet-name>JasperPrintServlet</servlet-name>
<servlet-class>servlets.JasperPrintServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>JasperPrintPDFServlet</servlet-name>
<servlet-class>servlets.JasperPrintPDFServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>Xml4SwfServlet</servlet-name>
<servlet-class>net.sf.jasperreports.j2ee.servlets.Xml4SwfServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>AppletServlet</servlet-name>
<servlet-class>servlets.AppletServlet</servlet-class>
</servlet>
<servlet>
<servlet-name>FlasthServlet</servlet-name>
<servlet-class>servlets.FlasthServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>PdfServlet</servlet-name>
<url-pattern>/servlets/pdf</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>JasperPrintServlet</servlet-name>
<url-pattern>/servlets/jasperprint</url-pattern>
</servlet-mapping>
<!-- 自定义Servlet -->
<servlet-mapping>
<servlet-name>JasperPrintPDFServlet</servlet-name>
<url-pattern>/servlets/jasperprintpdf</url-pattern>
</servlet-mapping>

<servlet-mapping>
<servlet-name>Xml4SwfServlet</servlet-name>
<url-pattern>/servlets/xml4swf</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>AppletServlet</servlet-name>
<url-pattern>/servlets/AppletServlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>FlasthServlet</servlet-name>
<url-pattern>
/servlets/flasthservlet</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
</web-app>

4、FlasthServlet.java
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
ServletContext context = this.getServletConfig().getServletContext();

/**
* 数据源
*/
System.out.println("数据源建立");
try {

System.out.println("数据源建立");
String url = "jdbc:mysql://localhost:3306/trms";
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection(url, "root", "root");
System.out.println("数据源建立成功");

} catch (SQLException e1) {
System.out.println("建立连接错误 = " + e1.toString());
e1.printStackTrace();
} catch (ClassNotFoundException e2) {
System.out.println("建立连接错误 = " + e2.toString());
e2.printStackTrace();
}

System.out.println("数据源链接1");

Connection conn = this.createConnection();
System.out.println("数据源链接2");

//request.setCharacterEncoding("gb2312");
//response.setCharacterEncoding("gb2312");
String reportid = request.getParameter("reportid");
System.out.println(reportid);

Map parameters = new HashMap();
parameters.put("preportid1", reportid);// 定义参数

Map parameters2 = new HashMap();
parameters2.put("preportid2", reportid);

Map parameters3 = new HashMap();
parameters3.put("preportid3", reportid);// 定义参数

Map parameters4 = new HashMap();
parameters4.put("preportid4", reportid);// 定义参数

Map parameters5 = new HashMap();
parameters5.put("preportid5", reportid);// 定义参数

Map parameters6 = new HashMap();
parameters6.put("preportid6", reportid);// 定义参数

Map parameters7 = new HashMap();
parameters7.put("preportid7", reportid);// 定义参数

Map parameters8 = new HashMap();
parameters8.put("preportid8", reportid);// 定义参数

Map parameters9 = new HashMap(); // 定义参数
parameters9.put("preportid9", reportid);

System.out.println();
System.out.println("JasperPrintServlet");

JasperPrint jasperPrint = null;
JasperPrint jasperPrint2 = null;
JasperPrint jasperPrint3 = null;
JasperPrint jasperPrint4 = null;
JasperPrint jasperPrint5 = null;
JasperPrint jasperPrint6 = null;
JasperPrint jasperPrint7 = null;
JasperPrint jasperPrint8 = null;
JasperPrint jasperPrint9 = null;
List<JasperPrint> jlist = new ArrayList<JasperPrint>();// 创建序列
try {
//报表数据填充
String filePath = "D://ireport//oldtower//tower1.jasper";
String filePath2 = "D://ireport//oldtower//tower2.jasper";
String filePath3 = "D://ireport//oldtower//tower3.jasper";
String filePath4 = "D://ireport//oldtower//tower4.jasper";
String filePath5 = "D://ireport//oldtower//tower5.jasper";
String filePath6 = "D://ireport//oldtower//tower6.jasper";
String filePath7 = "D://ireport//oldtower//tower7.jasper";
String filePath8 = "D://ireport//oldtower//tower8.jasper";
String filePath9 = "D://ireport//oldtower//tower9.jasper";

jasperPrint = JasperFillManager.fillReport(filePath, parameters,
conn);
System.out.println("jasperPrint1准备输出");
jasperPrint2 = JasperFillManager.fillReport(filePath2, parameters2,
conn);
System.out.println("jasperPrint2准备输出");

jasperPrint3 = JasperFillManager.fillReport(filePath3, parameters3,
conn);
System.out.println("jasperPrint3准备输出");
jasperPrint4 = JasperFillManager.fillReport(filePath4, parameters4,
conn);
System.out.println("jasperPrint4准备输出");
jasperPrint5 = JasperFillManager.fillReport(filePath5, parameters5,
conn);
System.out.println("jasperPrint5准备输出");
jasperPrint6 = JasperFillManager.fillReport(filePath6, parameters6,
conn);
System.out.println("jasperPrint6准备输出");
jasperPrint7 = JasperFillManager.fillReport(filePath7, parameters7,
conn);
System.out.println("jasperPrint7准备输出");
jasperPrint8 = JasperFillManager.fillReport(filePath8, parameters8,
conn);
System.out.println("jasperPrint8准备输出");
jasperPrint9 = JasperFillManager.fillReport(filePath9, parameters9,
new JREmptyDataSource());
// Jasper
System.out.println("jasperPrint9准备输出");

jlist.add(jasperPrint);
jlist.add(jasperPrint2);
jlist.add(jasperPrint3);
jlist.add(jasperPrint4);
jlist.add(jasperPrint5);
jlist.add(jasperPrint6);
jlist.add(jasperPrint7);
jlist.add(jasperPrint8);
jlist.add(jasperPrint9);

System.out.println("jasperPrint序列jlist添加成功准备输出");





request.getSession().setAttribute(BaseHttpServlet.DEFAULT_JASPER_PRINT_SESSION_ATTRIBUTE, jlist);
//request.getRequestDispatcher("../page/rflash.jsp").forward(request, res);
response.sendRedirect("../page/rflash.jsp");
} catch (JRException e) {
// TODO Auto-generated catch block
System.out.println("JRException");
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
System.out.println("IOException");
e.printStackTrace();
}



}

/**
* 建立连接方法
*
* @return Connection
*/
public Connection createConnection() {
Connection conn;
try {
String driver = "com.mysql.jdbc.Driver";
String url = "jdbc:mysql://localhost:3306/trms";
Class.forName(driver);
conn = DriverManager.getConnection(url, "root", "root");
conn.setAutoCommit(false);
return conn;
} catch (SQLException e1) {
System.out.println("建立连接错误 = " + e1.toString());
e1.printStackTrace();
} catch (ClassNotFoundException e2) {
System.out.println("建立连接错误 = " + e2.toString());
e2.printStackTrace();
}
return null;
}

/**
* 关闭连接方法
*
* @param conn
*/
public void CloseConnect(Connection conn) {
try {
conn.commit();
conn.setAutoCommit(true);
conn.close();
} catch (Exception e) {
System.out.println("关闭连接错误 = " + e.toString());
}
}


求大牛们指点,如果有相关的能够分享的案例就更好不过了。。实在不想做伸手党,不过实在是测了很久,也没有成果。QQ958749861
...全文
751 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
liujinmin1002 2015-05-02
  • 打赏
  • 举报
回复
牛!不过貌似这个需求我用finereport也能做啊,而且还不用这么多代码~
RisonGao 2014-04-19
  • 打赏
  • 举报
回复
引用 5 楼 rui888 的回复:
估计做的人少哦。
是很少,哈哈,I get it
RisonGao 2014-04-19
  • 打赏
  • 举报
回复
引用 4 楼 defonds 的回复:
你这是在分享吗
额 真心不是。。。最近下载资料 下载分数不够了。。用论坛积分兑换而已。。。
tony4geek 2014-04-15
  • 打赏
  • 举报
回复
估计做的人少哦。
Defonds 2014-04-15
  • 打赏
  • 举报
回复
你这是在分享吗
RisonGao 2014-04-15
  • 打赏
  • 举报
回复
@版主,没人回答我怎么结贴。。。回答一下,我发下分。。。。
RisonGao 2014-03-09
  • 打赏
  • 举报
回复
再次感谢
RisonGao 2014-03-09
  • 打赏
  • 举报
回复
五、applet显示部分 1、输入报告编号页面 1.html
    <form method="get" action="2.jsp">
    <input type="text" name="reportid">
    <input type="submit" value=2.jsp>
    </form>
2、applet显示页面2.jsp
	<%
			String sql = request.getParameter("reportid");
			System.out.println("viewer.jsp  =========:"+sql);
			String reportid="1001";
			session.setAttribute("reportid", reportid);
		%>
		Your name is:<%=request.getParameter("reportid")%>
		
		
		
			<!--"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="applets">
			<PARAM NAME=ARCHIVE
				VALUE="jasperreports-applet-5.2.0.jar,commons-logging-1.1.1.jar,commons-collections-2.1.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-applet-5.2.0.jar,commons-logging-1.1.1.jar,commons-collections-2.1.1.jar"
				\
            WIDTH="100%" 
            	\
            HEIGHT="100%"
				\
            CODE="ViewerApplet.class"
				\
            CODEBASE="applets"
				\
            ARCHIVE="jasperreports-applet-5.2.0.jar,commons-logging-1.1.1.jar,commons-collections-2.1.1.jar"
				\
            scriptable="false"
				\
            sql="<%=sql%>"
				\
            scriptable=false
			pluginspage="http://java.sun.com/products/plugin/index.html#download">
				<noembed>
					</XMP>
				</noembed>
			</embed>
			</comment>
		</object>
3、ViewerApplet.java
import java.awt.BorderLayout;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

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;

	@SuppressWarnings("unchecked")
	public void init() {

		try {
			//获取viewer.jsp的sql参数
			String sql = getParameter("sql");
			//String sql="1001";
			//使用URLEncoder对sql语句进行encode
			sql = URLEncoder.encode(sql, "UTF-8");
			//提交到servlet,使用servlet访问服务器端数据
			URL url = new URL(getCodeBase(), "../servlets/AppletServlet?sql="
					+ sql);
			if (url != null) {
				//获取服务器的传递过来的JasperPrint对象
				//JasperPrint jasperPrint = (JasperPrint) JRLoader
				//		.loadObject(url);
				//创建一个JRViewer
				// JRViewer viewer = new JRViewer(jasperPrint);
				 
				 List<JasperPrint> jasplist=(ArrayList<JasperPrint>)JRLoader.loadObject(url);
				 JRViewer viewer =new JRViewer((JasperPrint) jasplist);
				 
				 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);
	}

}
4、AppletServlet.java servlet处理 由于字数限制 已经删除非重要代码

 
package servlets;


public class AppletServlet extends HttpServlet {
	ToUTF8 StringToUTF8=new ToUTF8();
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(request, response);

	}

	
	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		// dopost开始
		ServletContext context = this.getServletConfig().getServletContext();
	/**
		 * 数据源
		 */

		try {

			System.out.println("数据源建立");
			String url = "jdbc:mysql://localhost:3306/trms";
			Class.forName("com.mysql.jdbc.Driver");
			Connection conn = DriverManager.getConnection(url, "root", "root");
			System.out.println("数据源建立成功");

		} catch (SQLException e1) {
			System.out.println("建立连接错误 = " + e1.toString());
			e1.printStackTrace();
		} catch (ClassNotFoundException e2) {
			System.out.println("建立连接错误 = " + e2.toString());
			e2.printStackTrace();
		}

		System.out.println("数据源链接1");

		Connection conn = this.createConnection();
		System.out.println("数据源链接2");

		request.setCharacterEncoding("gb2312");
		response.setCharacterEncoding("gb2312");
		
		String reportid = request.getParameter("sql");
		System.out.println(reportid);

		Map parameters = new HashMap();
		parameters.put("preportid1", reportid);// 定义参数

		Map parameters2 = new HashMap();
		parameters2.put("preportid2", reportid);

		Map parameters3 = new HashMap();
		parameters3.put("preportid3", reportid);// 定义参数

		Map parameters4 = new HashMap();
		parameters4.put("preportid4", reportid);// 定义参数

		Map parameters5 = new HashMap();
		parameters5.put("preportid5", reportid);// 定义参数

		Map parameters6 = new HashMap();
		parameters6.put("preportid6", reportid);// 定义参数

		Map parameters7 = new HashMap();
		parameters7.put("preportid7", reportid);// 定义参数

		Map parameters8 = new HashMap();
		parameters8.put("preportid8", reportid);// 定义参数

		Map parameters9 = new HashMap(); // 定义参数
		parameters9.put("preportid9", reportid);

		System.out.println();
		System.out.println("JasperPrintServlet");

		JasperPrint jasperPrint = null;
		JasperPrint jasperPrint2 = null;
		JasperPrint jasperPrint3 = null;
		JasperPrint jasperPrint4 = null;
		JasperPrint jasperPrint5 = null;
		JasperPrint jasperPrint6 = null;
		JasperPrint jasperPrint7 = null;
		JasperPrint jasperPrint8 = null;
		JasperPrint jasperPrint9 = null;
		List<JasperPrint> jlist = new ArrayList<JasperPrint>();// 创建序列
		try {

			String filePath = "D://ireport//oldtower//tower1.jasper";
			String filePath2 = "D://ireport//oldtower//tower2.jasper";
			String filePath3 = "D://ireport//oldtower//tower3.jasper";
			String filePath4 = "D://ireport//oldtower//tower4.jasper";
			String filePath5 = "D://ireport//oldtower//tower5.jasper";
			String filePath6 = "D://ireport//oldtower//tower6.jasper";
			String filePath7 = "D://ireport//oldtower//tower7.jasper";
			String filePath8 = "D://ireport//oldtower//tower8.jasper";
			String filePath9 = "D://ireport//oldtower//tower9.jasper";

			jasperPrint = JasperFillManager.fillReport(filePath, parameters,
					conn);
			System.out.println("jasperPrint1准备输出");
			jasperPrint2 = JasperFillManager.fillReport(filePath2, parameters2,
					conn);
			System.out.println("jasperPrint2准备输出");

			jasperPrint3 = JasperFillManager.fillReport(filePath3, parameters3,
					conn);
			System.out.println("jasperPrint3准备输出");
			jasperPrint4 = JasperFillManager.fillReport(filePath4, parameters4,
					conn);
			System.out.println("jasperPrint4准备输出");
			jasperPrint5 = JasperFillManager.fillReport(filePath5, parameters5,
					conn);
			System.out.println("jasperPrint5准备输出");
			jasperPrint6 = JasperFillManager.fillReport(filePath6, parameters6,
					conn);
			System.out.println("jasperPrint6准备输出");
			jasperPrint7 = JasperFillManager.fillReport(filePath7, parameters7,
					conn);
			System.out.println("jasperPrint7准备输出");
			jasperPrint8 = JasperFillManager.fillReport(filePath8, parameters8,
					conn);
			System.out.println("jasperPrint8准备输出");
		

			jasperPrint9 = JasperFillManager.fillReport(filePath9, parameters9,
					new JREmptyDataSource());
			// Jasper
			System.out.println("jasperPrint9准备输出");

			jlist.add(jasperPrint);
			jlist.add(jasperPrint2);
			jlist.add(jasperPrint3);
			jlist.add(jasperPrint4);
			jlist.add(jasperPrint5);
			jlist.add(jasperPrint6);
			jlist.add(jasperPrint7);
			jlist.add(jasperPrint8);
			jlist.add(jasperPrint9);

			System.out.println("jasperPrint序列jlist添加成功准备输出");

		} catch (JRException e) {
			

			e.printStackTrace(out);

		
			return;
		}

		if (jlist != null) {
			

			try {
				response.setContentType("application/octet-stream");
				ServletOutputStream out = response.getOutputStream();
				ObjectOutputStream os = new ObjectOutputStream(out);
				os.writeObject(jlist);
				os.flush();
				os.close();
			} catch (Exception e1) {
				e1.printStackTrace();
			} 

		} else {
			
		}
	}

	/**
	 * 建立连接方法
	 * 
	 * @return Connection
	 */
	public Connection createConnection() {
		Connection conn;
		try {
			String driver = "com.mysql.jdbc.Driver";
			String url = "jdbc:mysql://localhost:3306/trms";
			Class.forName(driver);
			conn = DriverManager.getConnection(url, "root", "root");
			conn.setAutoCommit(false);
			return conn;
		} catch (SQLException e1) {
			System.out.println("建立连接错误 = " + e1.toString());
			e1.printStackTrace();
		} catch (ClassNotFoundException e2) {
			System.out.println("建立连接错误 = " + e2.toString());
			e2.printStackTrace();
		}
		return null;
	}

	/**
	 * 关闭连接方法
	 * 
	 * @param conn
	 */
	public void CloseConnect(Connection conn) {
		try {
			conn.commit();
			conn.setAutoCommit(true);
			conn.close();
		} catch (Exception e) {
			System.out.println("关闭连接错误 = " + e.toString());
		}
	}

	

	// dopost结束

}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧