File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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 Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet OutputStream" Watch "Servlet OutputStream" New topic

Servlet OutputStream

vishnu priya

Joined: Nov 06, 2009
Posts: 13
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 =
InputStream reportStream =
connection = DriverManager.getConnection ("jdbc:mysql://
servletOutputStream, new HashMap(), connection);
catch (Exception e)
// display stack trace in the browser
StringWriter stringWriter = new StringWriter();
PrintWriter printWriter = new PrintWriter(stringWriter);

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.

So much trouble in the world -- Bob Marley
Bear Bibeault
Author and ninkuma

Joined: Jan 10, 2002
Posts: 63873

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] [About Bear] [Books by Bear]
Ben Souther

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.

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

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.
I agree. Here's the link:
subject: Servlet OutputStream
It's not a secret anymore!