JSP using a bean to do output?
One thing I haven't seen much mention of is the possibility of using a Java Bean to
do some output for a JSP. I'm porting a page I wrote a while back that uses a
JHTML page to call a servlet which is just a thin wrapper around a bean which
actually writes the HTML output (the bean is actually a network client to a server
that generates the HTML page). The natural line of attack seems to be to have a
JSP call the bean which will write the output, but I haven't seen mention of that
technique anywhere and wonder why I'm so strange in thinking of this. The bean
method to do the output currently takes a PrintWriter as a parameter to output to,
but that can be changed to something similar if desired.
Can anyone give me a snippet of code that would best implement taking the output
stream for a JSP page and passing it to a bean for output?
Thanks
Steve
[946 byte] By [
sprior913] at [2007-9-26 3:30:51]

eh? It sounds like your bean is doing all the output? If that's the case, then why do you want to use a JSP? - use a servlet as you already are.
If you have a bean declared in the page and you just want to pass it the page's output stream to output part of the pages content, then use this:
<jsp:useBean id='myBean' class='com.mycompany.MyBeanClass'/>
...
<%
PrintWriter beanOut = new PrintWriter(out);
myBean.writeContent(beanOut);
beanOut.close()
%>
...
where writeContent is your method that takes a PriontWriter. out is a JspWriter, which extends Writer, so you need to create a PrintWriter from it. You may or may not need to close/flush the PrintWriter deppending upon your bean implementation.
Thanks for the help!
Yes I could keep things pretty much the way they are, but right now I have 3 elements for
the page - a jhtml file, a servlet, and a bean. It would just be convenient to have 2 things
to deal with instead of 3 things. Also, part of the reason I asked the question was as a
learning experience to see if there was some well known reason why I should just plain
NOT pass the output stream to the bean, and it appears that there isn't, it's just not something
I've seen mention or examples of.
Steve