File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes Servlet OutputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet OutputStream" Watch "Servlet OutputStream" New topic
Author

Servlet OutputStream

vishnu priya
Greenhorn

Joined: Nov 06, 2009
Posts: 13
Hi
I am using the servlet outputstream in my javaclass as shown below.
public class DbReportServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException
{
Connection connection;
ServletOutputStream servletOutputStream =
response.getOutputStream();
InputStream reportStream =
getServletConfig().getServletContext().getResourceAsStream
("/reports/DbReport.jasper");
try
{
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection ("jdbc:mysql://
localhost:3306/flightstats?user=dbuser&password=secret");
JasperRunManager.runReportToPdfStream(reportStream,
servletOutputStream, new HashMap(), connection);
connection.close();
response.setContentType("application/pdf");
servletOutputStream.flush();
servletOutputStream.close();
}
catch (Exception 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 i need to redirect to another jsp and display the outputstream content in the jsp page.
I m stuck with this problem.
Please help me out....
Thanks in advance.
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Any particular reason why you are trying to use a redirect and not a forward? For forwarding you need to get a hold of the requestDispatcher object.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60789
    
  65

Please be sure to use code tags when posting code to the forums. Unformatted or unindented code is extremely hard to read and many people that might be able to help you will just move along to posts that are easier to read. Please click this link ⇒ UseCodeTags ⇐ for more information.

Properly indented and formatted code greatly increases the probability that your question will get quicker, better answers.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

You're approaching this from the wrong end.
You can only send one response from a request.
If you're going to write to the servlet output stream, you can't also forward or redirect.

Build your servlet so that it can stream the binary data (PDF) to the browser.
Then build your JSP. Construct your links so that the user hits the JSP first.
In your JSP have a link, or iframe, or whatever point to the servlet that streams the PDF.

We have an example project all 'war'ed up in our our code barn.
It streams an image instead of a generated PDF but the flow is exactly the same.

http://faq.javaranch.com/java/CodeBarnSimpleStream


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

BTW: To back up Bear's point.

I did see this post this morning but skipped by it when I saw the unformatted code.
I'm guessing I'm not the only person who saw this question but didn't bother to read the whole thing.
 
 
subject: Servlet OutputStream
 
Similar Threads
problem implementing iReports with servlets
Error:getOutputStream() has already been called for this response
How to call a servlet from JSF without using dispatch(url)
Problem with jasper reports
JasperReport error while connecting from Struts action class