How to pass output stream to method (JSP)?

K

kaeli

Hi all,

I am writing a helper class that will be included in either a JSP page
or in a command line application.
The problem is that System.out only works for command line. It shoves it
off to some other stream (I have yet to find where, but I think it's the
server standard error) when used with jsp.

So, I want to pass the output stream to the method as follows
public void methodName(outputstream)

What class should I use for outputstream?
For JSP, I will pass it as out.
methodName(out);
For command line, I will pass it as System.out.
methodName(System.out);

Then in the method, I want to use
outputstream.println("whatever");

Note that the class is NOT a servlet, so I have no access to servlet
methods like pageContext.getOutputStream.

TIA
--
--
~kaeli~
If that phone was up your a$$, maybe you could drive a
little better!
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
K

kaeli

To clarify...
So, I want to pass the output stream to the method as follows
public void methodName(outputstream)

What class should I use for outputstream?
For JSP, I will pass it as out.

JSP out object is a PrintWriter.
It comes from java.io.Writer.

methodName(out);
For command line, I will pass it as System.out.

System.out is a PrintStream.
It comes from java.io_OutputStream.

There is no common class these extend, so I can't think of what object
to use for polymorphism (println).
I can't just pass Object, can I?

I'm still a bit new here.
Can I do
myObj.println
no matter which class I pass in if I protoyped as Object?

Public myMethod (Object out)
{
out.println("some output");
}

I thought I needed to cast to do that, since Object has no println
method. If so, I have no way of knowing which object to cast
TO...PrintStream or PrintWriter...

Anyone have an input on this?

--
 
J

Joona I Palaste

kaeli said:
To clarify...
JSP out object is a PrintWriter.
It comes from java.io.Writer.
System.out is a PrintStream.
It comes from java.io_OutputStream.
There is no common class these extend, so I can't think of what object
to use for polymorphism (println).
I can't just pass Object, can I?

You'll pretty much have to, and handle OutputStreams and
PrintWriters separately with instanceof checks.
It'll be _very_ cumbersome.
I'm still a bit new here.
Can I do
myObj.println
no matter which class I pass in if I protoyped as Object?
Public myMethod (Object out)
{
out.println("some output");
}

No, you can't do that. Object doesn't have a println method.
I thought I needed to cast to do that, since Object has no println
method. If so, I have no way of knowing which object to cast
TO...PrintStream or PrintWriter...

Yes you do. Try "if (out instanceof PrintStream)" or "if (out
instanceof PrintWriter)".
Anyone have an input on this?

Me, I'd create a new class called "PrintWrapper" which would have a
println method, but it would be a wrapper around either a PrintStream
or a PrintWriter, and the println method would delegate to those.
Something like:

public class PrintWrapper {
private PrintStream s;
private PrintWriter w;
public PrintWrapper(PrintStream s) {
this.s=s;
}
public PrintWrapper(PrintWriter w) {
this.w=w;
}
public void println(String text) {
if (s!=null) s.println(text);
else w.println(text);
}
/* and so on */
}

but that is an advanced solution, so if you're not comfortable with
it, you're better off with instanceof checks.
 
A

Andrew Thompson

....
| I am writing a helper class that will be included in either a
JSP page
| or in a command line application.
| The problem is that System.out only works for command line. It
shoves it
| off to some other stream (I have yet to find where, but I think
it's the
| server standard error) when used with jsp.

Why not have the helper class return a String,
but simply print the String when you are in
the console?
 
K

kaeli

andrew64 said:
Why not have the helper class return a String,
but simply print the String when you are in
the console?

Because the string would be WAY too long.
The class is a wrapper that takes a result set and makes it a crosstab
and outputs html. The results of a query can be hundreds of rows long,
though most commonly they will be under 100. But I have no way of
knowing how long the string will be until runtime.

--
 
K

kaeli

Yes you do. Try "if (out instanceof PrintStream)" or "if (out
instanceof PrintWriter)".

Ooh. I like that.
Me, I'd create a new class called "PrintWrapper" which would have a
println method, but it would be a wrapper around either a PrintStream
or a PrintWriter, and the println method would delegate to those.
Something like:
but that is an advanced solution, so if you're not comfortable with
it, you're better off with instanceof checks.

I'd have no clue what to do with it or how to use it. :)
But thanks!
(hey, can I do that with an inner class? this is only needed for one
class.)


--
 
A

Andrew Thompson

| In article <[email protected]>,
andrew64
| @bigNOSPAMpond.com enlightened us with...
| >
| > Why not have the helper class return a String,
| > but simply print the String when you are in
| > the console?
....
| Because the string would be WAY too long.

Are you _sure_? I have loaded entire
10+Meg files into a String without any
difficulty..
 
C

Christophe Vanfleteren

Andrew said:
| In article <[email protected]>,
andrew64
| @bigNOSPAMpond.com enlightened us with...
| >
| > Why not have the helper class return a String,
| > but simply print the String when you are in
| > the console?
...
| Because the string would be WAY too long.

Are you _sure_? I have loaded entire
10+Meg files into a String without any
difficulty..

Indeed, since a String can hold Integer.MAX (2^31-1) characters, your String
won't get too large fast.
Just returning a string (created with a StringBuffer offcourse !) seems like
the best thing the OP could do.
 
K

kaeli

Indeed, since a String can hold Integer.MAX (2^31-1) characters, your String
won't get too large fast.
Just returning a string (created with a StringBuffer offcourse !) seems like
the best thing the OP could do.

Actually, what I did was similar to this.

http://www.utexas.edu/its/eis/javaig/archive/2003/summer/ben/

-------

writeName(Writer writer){

writer.write(System.getProperty(“user.name”) [sic]

}

I could pass this method a JSPWriter, or I could pass it new
PrintWriter(System.out) . The possibilities boggle the mind!


But I can't get this simple test to output...
I'm confused.

import java.io.*;

public class testIO
{
public static void main(String []args)
{
try
{
PrintWriter o = new PrintWriter(System.out);
o.write("test\n");
}
catch (Exception e)
{
System.out.println("Exception:"+e);
System.out.println(e.getMessage());
e.printStackTrace();
}
}
}

It doesn't output anything.
What am I doing wrong?

--
--
~kaeli~
A midget fortune teller who escapes from prison is a small
medium at large.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
T

Thomas Weidenfeller

kaeli said:
Can I make a PrintWriter object from a PrintStream?

It might not make to much sense, but you can of course do:

PrintWriter pw = new PrintWriter(somePrintStream);

because PrintStream is a subclass of OutputStream.

You can find out such things yourself simply by studying the API
documentation for a few seconds.

/Thomas
 
K

kaeli

It doesn't output anything.
What am I doing wrong?

NM.
o.flush fixed it.

For the archives, the finished method was

public void outputColumnHeadersHtml(Writer o) throws
java.io.IOException
{
o.write("\n");
o.write("<tr>\n");
// need one blank column, which will line up with the row headers
o.write("<th>&nbsp</th>");
Iterator i = this.COLUMN_V.iterator();
while (i.hasNext())
{
o.write("<th>"+(String)i.next() + "</th>\n");
}
o.write("\n</tr>\n");
o.flush();
}

Called in JSP with
<%
crosstab.outputColumnHeadersHtml(out);
%>

and in command line with
PrintWriter o = new PrintWriter(System.out);
crosstab.outputColumnHeadersHtml(o);

Thanks to all!
--
 
K

kaeli

It might not make to much sense, but you can of course do:

PrintWriter pw = new PrintWriter(somePrintStream);

because PrintStream is a subclass of OutputStream.

You can find out such things yourself simply by studying the API
documentation for a few seconds.

Not really.
I have hard time with the docs. I do actually read them, not that it
helps much.

I did solve my problem with the help of a nice internet page.

Thanks.

--
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top