Shao wrote:
>
> Hi,
>
> I had to make output to broswer in bean for some reasons. i did like this:
> test1.html:
>    <% out.println("step1"); %>
>    <jsp:useBean id="Func" scope="session" class="genufunc.func" />
>    <%
>     Func.testoutput(response);
>     %>
>
> func.java:
>    package genufunc;
>    import java.io.*;
>    import java.util.*;
>    import javax.servlet.*;
>    import javax.servlet.http.*;
>    public class func {
>      public void testoutput(HttpServletResponse response) {
>        HttpServletResponse Response;
>        PrintWriter out;
>        Response = response;
>        try {
>          out = Response.getWriter();
>        }
>        catch(IOException ex){ ex.printStackTrace();}
>        out.println("step2");
>      }
>   }
>
> But the output result in broswer was: "step2 step1" and not  "step1 step2"
> as i image.
> How can i do in right order?

The reason the output comes in the wrong order is because you're writing
directly to the response's output stream (the Writer returned by getWriter())
in the bean. That's a no-no according to the spec. The JSP container writes
to an instance of the JspWriter class, which buffers the output before it
sends it to the browser through the response's output stream. When you
write directly to the response's output stream, your output is sent before
the buffered output.

The easiest way to fix your example is to pass the JSP out object (the
JspWriter)
to the bean instead of the the response object, and then use the out object
for all output produced by the bean. A better way, IMHO, is to change the
bean into a custom action. A custom action has access to the JspWriter
automatically, so you don't need to pass it explicitly. With a custom action
you can also get rid of the scriptlet that invokes the bean, since a custom
action is asked to do its thing by the JSP container. The end result could
look like this:

  step1 <!-- No need to use a scriptlet for writing static text -->
  <foo:myAction />

where the custom action generates whatever output you want. The action
tag handler, following your example, looks something like this:

  package genufunc;
  import java.io.*;
  import javax.servlet.jsp.tagext.*;
  public class FuncTag extends TagSupport {
    public int doEndTag() {
      JspWriter out = pageContext.getOut();
      try {
        out.println("step2");
      }
      catch (IOException e) {}
      return EVAL_PAGE;
    }
 }


Hans
--
Hans Bergsten           [EMAIL PROTECTED]
Gefion Software         http://www.gefionsoftware.com
Author of JavaServer Pages (O'Reilly), http://TheJSPBook.com

===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
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

Reply via email to