Streaming contents with JSP

Discussions

Web tier: servlets, JSP, Web frameworks: Streaming contents with JSP

  1. Streaming contents with JSP (7 messages)

    Hi,

    My JSP needs to stream msword documents back to the user.

    Is that just a matter of doing a response.setContentType("application/msword") and outputing the bytes?

    What do I do with the ContenType attribute of the page directive?

    Thanks...

    Threaded Messages (7)

  2. Streaming contents with JSP[ Go to top ]

    Just want to clarify myself. I'd like to read a word document and send it back to the user at a browser.

    Thanks...
  3. Streaming contents with JSP[ Go to top ]

    well, it was as simple as using setContentType and

    byte[] data = new byte(10000);
    OuputStream ostream = response.getOutputStream();
    ostream.write(data);

    Although it worked, but I still get IllegalStateException, and I determined that it was due to the fact that the JSP container already called getWriter() to initialize 'out' and one can only call either getOuputStream or get Writer but not botnh....

    Anyone knows how to prevent the JSP container from obtaining a writer before I obtain my outputstream?

    Thanks...

  4. Streaming contents with JSP[ Go to top ]

    你可以用res.setContentType("application/vnd-msword")
  5. Streaming contents with JSP[ Go to top ]

    Why application/vnd-msword and not application/msword?

  6. Streaming contents with JSP[ Go to top ]

    What you're trying to do is illegal according to the JSP 1.2 spec; from page 42 (note particularly the last sentence):

    "Initially, out is a new JspWriter object. This object may be different from the stream object returned from response.getWriter(), and may be considered to be interposed on the latter in order to implement buffering (see Section JSP.2.10.1, “The page Directive”). This is the initial out object. JSP page authors are prohibited from writing directly to either the PrintWriter or OutputStream associated with the ServletResponse."

    However, it appears that your servlet/JSP container may be misbehaving. It should be possible for you to do a forward from your JSP page to a servlet that will return the Word document as binary data. However, if your servlet/JSP container has already invoked response.getWriter(), then the forward to the servlet won't work. Again, from page 42 of the JSP 1.2 spec:

    "The JSP container should not invoke response.getWriter() until the time when the first portion of the content is to be sent to the client. This enables a number of uses of JSP, including using JSP as a language to ‘glue’ actions that deliver binary content, or reliably forwarding to a servlet, or change dynamically the content type of the respose before generating content. See Chapter JSP.3."
  7. Streaming contents with JSP[ Go to top ]

    I doubt that I am doing anything illegal. The Javasoft web page lists exactly what I am doing.

    The point here is that at no point do I call response.getWriter().

    It seems like the JSP container called response.getWriter() to initalize the out object regardless of the declared contentType in the page directive - and that I think is the problem.
  8. Status[ Go to top ]

    Well, besides what we already know, is there any solution for this issue?