Of course, the easiest solution is to use the PrintWriter to wrap a
StringWriter and keep the StringWriter instance handy so you can call the
toString method.
Example:
StringWriter str = new StringWriter();
PrintWriter out = new PrintWriter(str);
// Print Stuff using out.println Here
return str.toString()
(*Chris*)
----- Original Message -----
From: Mukhar, Kevin <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Thursday, June 17, 1999 1:19 PM
Subject: Re: Error Handeling Methods
> As in many situations, your code is doing exactly what you coded it to do.
>
> Let's jump to the end of your code to this line:
> return writer.toString();
>
> This calls the toString() method of the class. Since PrintWriter doesn't
> define a method toString, you walk up the inheritance chain until you get
to
> the parent class that does define the method, in this case the Object
class.
> For the Object class, the documentation for the method toString says "The
> toString method for class Object returns a string consisting of the name
of
> the class of which the object is an instance, the at-sign character `@',
and
> the unsigned hexadecimal representation of the hash code of the object."
So,
> it's printing out the hash code because that's what you told it to do.
>
> The other reason that it doesn't work is that PrintWriter does not hold
the
> string you give it so that you can retrieve it at a later time.
PrintWriter
> sends stuff to a stream of some kind and does not retain what you give it.
> This line of code creates a PrintWriter instance which will write what you
> give it to the OutputStream defined by System.out:
>
> > PrintWriter writer = new PrintWriter(System.out);
> >
> So every line of code like
>
> > writer.println(e.nextElement().toString());
> >
> sends a string to System.out.
>
> There are probably several approaches to get what you want. Here are two
> suggestions:
>
> 1. Throw out all the PrintWriter stuff. Inside your method create a
> StringBuffer object. Append all the information you want to the
StringBuffer
> object. Return StringBuffer.toString(). This gives you a String that you
can
> send back to the browser with the HttpServletResponse object.
>
> 2. Send the HttpServletResponse object to the method. Have the method send
> the HTML page directly back to the browser using the response object.
>
> K Mukhar
>
> > ----------
> > From: Java List[SMTP:[EMAIL PROTECTED]]
> > Reply To: A mailing list for discussion about Sun Microsystem's Java
> > Servlet API Technology.
> > Sent: Thursday, June 17, 1999 2:33 PM
> > To: [EMAIL PROTECTED]
> > Subject: Error Handeling Methods
> >
> > I've written two static methods for handeling errors in my Servlets.
The
> > return value is a PrintWriter object which contains the errors
formattted
> > in an HTML page. One of the methods takes a single (String) error as
> > it's arguement, and the other takes multiple errors (Vector) as its
> > arguement. When I try to print this returned PrintWriter object in my
> > Servlet, I'm getting a hash code rather than a String - ( i.e.
> > java.io.PrintWriter@b44b5d6f)
> >
> > Any Suggestions???
> >
> > -Nash
> >
> >
> > Here's one of the error handeling method:
> > ---------
> > public static String getMultipleErrorsAsString(Vector error) {
> >
> > PrintWriter writer = new PrintWriter(System.out);
> > Header errorHead = new Header("Error");
> > Body errorBody = new Body("#FFFFFF");
> > writer.println(errorHead.printBasicHeader());
> > writer.println(errorBody.printBasicBody());
> > for (Enumeration e = error.elements();
> > e.hasMoreElements();) {
> > writer.println(e.nextElement().toString());
> > }
> > writer.println("</body></html>");
> >
> > return writer.toString(); // <-- Why does this return
a
> > hash code
> > rather than a String?
> > }
> >
>
>
___________________________________________________________________________
> To unsubscribe, send email to [EMAIL PROTECTED] and include in the
body
> of the message "signoff SERVLET-INTEREST".
>
> Archives: http://archives.java.sun.com/archives/servlet-interest.html
> Resources: http://java.sun.com/products/servlet/external-resources.html
> LISTSERV Help: http://www.lsoft.com/manuals/user/user.html
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html