In a servlet, the objectives will be to:
* Set the content type correctly, so the browser
knows what you are sending
* Send a binary output stream, rather than text.
A model of how you'd do this would be to have this in your doGet() method:
public void doGet(HttpServletRequest req,
HttpServletResponse res)
throws IOException, ServletException {
res.setContentLength(xxx); // If you know how many bytes
res.setContentType("image/gif"); // Or whatever is appropriate
InputStream is = ...; // Input stream to your file
ServletOutputStream os = res.getOutputStream();
int b;
while ((b = is.read()) >= 0)
os.write(b);
os.flush();
}
NOTE: You will need to add a little bit of exception handling, and you
might need to optimize the performance -- but this is the general idea.
Craig
On Tue, 21 Mar 2000, Undetermined origin c/o LISTSERV administrator wrote:
> I hate to be so dense, but how would you do this? How would you use a
> servlet to download binary data?
>
> -----Original Message-----
> From: A mailing list about Java Server Pages specification and reference
> [mailto:[EMAIL PROTECTED]]On Behalf Of Craig R. McClanahan
> Sent: Wednesday, March 15, 2000 11:48 PM
> To: [EMAIL PROTECTED]
> Subject: Re: response.getOutputStream()
>
>
> SoftFrance RANSON Maxime wrote:
>
> > Can't I use the method
> > response.getOutputStream()
> > in a Jsp file ?
> >
> > I got the Error
> > Writer is already being used for this request, does anybody
> > solve this problem, where does it come from ?
> >
>
> It comes from the fact that the beginning of the service() method in the
> servlet
> generated for your JSP page has something like this in it:
>
> JspWriter out = response.getWriter();
>
> which means that the writer has already been grabbed. Based on the servlet
> specification, that means (therefore) that you cannot also access the output
> stream via response.getOutputStream() inside that same page. If you want to
> dynamically create output from scriptlets in your JSP page, all you need to
> do is
> use the already existing implicit variable:
>
> <%
> out.println("<b>This is some bold text!</b>");
> %>
>
> Alternatively, if you want to create binary output instead, you should use a
> real
> servlet.
>
> Craig McClanahan
>
> ===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>
> ===========================================================================
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets