[英]how do I display a jasper report in a web browser
Good morning everyone, my name is david, and am new in jasper reports (urm..am also new in stack over flow too).大家早上好,我的名字是 david,我是 jasper 报告的新成员(嗯……我也是堆栈溢出的新成员)。 I downloaded a tutorial on jasper reports that guided me through the procedures of creating my first report.
我下载了一个关于 jasper 报告的教程,它指导我完成创建第一个报告的过程。 I have successfully created my
.jrxml
file, compiled it to a .jasper
file and filled it to a .jprint
file, all with the aid of java codes, I have also previewed the report successfully with jasper viewer with the aid of ant targets in my build.xml
file.我已经成功创建了我的
.jrxml
文件,将其编译为.jasper
文件并将其填充为.jprint
文件,所有这些都借助 java 代码,我还借助 ant 目标在 jasper 查看器的帮助下成功预览了报告我的build.xml
文件。
My problem now is how to display the report on a web browser.我现在的问题是如何在网络浏览器上显示报告。
All that was being given to me in the tutorial was a java servlet code that's exports the jasper report to a pdf format which will then be displayed on the browser.教程中提供给我的只是一个 java servlet 代码,它将 jasper 报告导出为 pdf 格式,然后将在浏览器上显示。 Here is the code:
这是代码:
package net.ensode.jasperbook;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
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.JasperRunManager;
public class FirstReportSendToBrowserServlet extends. HttpServlet{
@SuppressWarnings("unchecked")
protected void doGet(HttpServletRequest. request, HttpServletResponse response)
throws ServletException, IOException{
ServletOutputStream servletOutputStream = response.getOutputStream();
InputStream reportStream = getServletConfig().getServletContext().getResourceAsStream("/reports/FirstReport.jasper");
try{
JasperRunManager.runReportToPdfStream(reportStream, servletOutputStream, new HashMap(), new JREmptyDataSource());
response.setContentType("application/pdf");
servletOutputStream.flush();
servletOutputStream.close();
}
catch(JRException e){
//display stack trace in the browser
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);
e.printStackTrace(printWriter);
response.setContentType("text/plain");
response.getOutputStream().print(stringWriter.toString());
}
}
}
Now my problem is how am going deploy the above code in a servlet continer.现在我的问题是如何在 servlet 容器中部署上述代码。 So it will display my jasper report in a pdf format on my web browser.
所以它会在我的网络浏览器上以 pdf 格式显示我的 jasper 报告。 The tutorial said an ant script to automate the process could be found at their website but I searched and did not find any..
该教程说可以在他们的网站上找到一个自动化该过程的蚂蚁脚本,但我搜索并没有找到任何..
So If anyone of you could give me instruction on how to deploy the above code in a servlet container, or give me a link that will instruct me on how to create the ant script that automate the process, I will be very very greatefull cause I have spent almost three days on this problem.因此,如果你们中的任何人都可以给我关于如何在 servlet 容器中部署上述代码的说明,或者给我一个链接来指导我如何创建自动化该过程的 ant 脚本,我将非常非常好,因为我在这个问题上花了将近三天的时间。
I recommend you to use an IDE such as eclipse.我建议您使用 IDE,例如 Eclipse。 Install this version that contains all the tools for web development.
安装此版本,其中包含用于 Web 开发的所有工具。
http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/keplersr1 http://www.eclipse.org/downloads/packages/eclipse-ide-java-ee-developers/keplersr1
Then you can create a "dynamic web project" and configure some servlet container (maybe tomcat - http://tomcat.apache.org/download-70.cgi ).然后你可以创建一个“动态 web 项目”并配置一些 servlet 容器(可能是 tomcat - http://tomcat.apache.org/download-70.cgi )。
And then (finally) you can right-click on your project and choose "export", and then, "war".然后(最后)您可以右键单击您的项目并选择“导出”,然后选择“战争”。 Save it directly into tomcat's webapp directory.
直接保存到tomcat的webapp目录下。
It will generate a file that you can just drop into your container and hopefully everything will be fine.它将生成一个文件,您可以将其放入容器中,希望一切都会好起来。
Of course, there's a learning curve here, but using an IDE will save your time later.当然,这里有一个学习曲线,但以后使用 IDE 会节省您的时间。
Check this tutorial - http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.stardust.docs.wst%2Fhtml%2Fwst-integration%2Fdynamic-web-proj.html检查本教程 - http://help.eclipse.org/kepler/index.jsp?topic=%2Forg.eclipse.stardust.docs.wst%2Fhtml%2Fwst-integration%2Fdynamic-web-proj.html
What you have to do is create a web project in your servlet container.您需要做的是在您的 servlet 容器中创建一个 Web 项目。 Under
Tomcat
this can be easily done just creating a subfolder under your webapps
folder with this structure (it is just an example):在
Tomcat
这可以很容易地完成,只需在您的webapps
文件夹下创建一个具有此结构的子文件夹(这只是一个示例):
webapps
|
+---projectName
|
+---WEB-INF
|
+---web.xml
|
+---classes
|
+---ServletClass.class
Where ServletClass.class
is your compiled servlet.其中
ServletClass.class
是您编译的 servlet。
web.xml
must contain the wepapp description, something like: web.xml
必须包含 wepapp 描述,例如:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
>
<servlet>
<servlet-name>ServletClass</servlet-name>
<servlet-class>ServletClass</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletClass</servlet-name>
<url-pattern>/url-to-servlet.pdf</url-pattern>
</servlet-mapping>
</web-app>
And restart your engine.并重新启动引擎。 Now, typing this URL n the browser should do de job:
现在,在浏览器中输入此 URL 即可完成工作:
http://server-address:8080/projectName/url-to-servlet.pdf
I solved my problem:我解决了我的问题:
So that's was all所以这就是全部
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.