Basically you are on the right track.
Write a servlet doGet for example to output HTML (or XHTML or anything else you
like) to a buffer. For (X)HTML a `StringWriter' will do, but HTTP Tunnelling
a `ByteArrayOutputStream' will do. At the very end of your process
you flush the buffer, and write the contents of it to the HttpResponseOutput .
doGet( HttpServletRequest req , HttpServletResponse res, )
{
try {
// Create StringWriter to buffer out HTML
StringWriter sw = new StringWriter();
PrinterWriter pw = PrintWriter( sw );
// Assume some process write a hefty web page
pw.println("<html><title>. Whatever.com </title>" );
doWhatever( pw );
pw.println("</body></html>" );
// Very End flush the string writer and collect the buffered web page
res.setContentType("text/html" );
pw.flush();
String webpage = sw.toString();
PrintWriter out = req.getWriter();
// Spit out the webpage
out.println( webpage );
out.flush();
out.close();
}
catch (Throwable t) {
// If an exception occur the above buffered web page is never seen
// instead a simply stack trace is dumped
res.setContentType("text/plain");
PrintWriter out = new PrintWriter( res.getWriter() );
out.println("Damn I got an exceptuion!!!!!!!!!" );
t.printStackTrace(out );
out.flush();
out.close();
}
}
Hope This Helps Somebody!
--
Peter Pilgrim
Welcome to the "Me Too" generation.
---------------------------------------- Message History
----------------------------------------
From: [EMAIL PROTECTED] on 28/09/2000 04:24
Please respond to [EMAIL PROTECTED]
To: [EMAIL PROTECTED]
cc:
Subject: Re: Lets talk about good exception handling!
>>> Marc Krisjanous <[EMAIL PROTECTED]> 28-Sep-00 5:24:33 AM
>>>
>Now, the perfromTask method throws an
>ExceptionController(extends Exception)
>instance which is caught in the doGet or doPost methods.
>Then I currently send the error back to the web browser.
> The question is: is this good exception handling??
It's perfectly acceptable.
Personally I'd do the work that performTask does inside service() and
catch all the different exceptions seperately.
The thing is that you can't use a formula for exception handling...
it's down to what you want to do at the time and in the
circumstances:
- handle any exception that could be produced in the same way...
- handle particular bits of code with special handlers
- handle all exceptions specifically
This is judgement... if you think hard about what you want to do now
it'll come and it'll get easier.
Something that can help is looking at good free software (open source
code) to see how the pros do it.
Here's a few pointers, but even these can't be taken too literally:
- always catch Throwable not Exception for generic exceptions
Throwable is actually the lowest exception... if you just catch
Exception your code will blow up if it gets a Throwable
- for small code use a single try/catch with specific exception
handlers
- for larger code use a try/catch around code that might blow up
- use the ServletContext.log method that allows writing stack traces
to the log
- with servlets think hard about the link between an exception and an
HTTP error
is it a 500? a 501? a 400? what?
Lastly, here's a code fragment that you can use to print stack traces
to an html page.
The Throwable is called: t.
The Writer connected to the response is called: m_out
//we need to do this to collect the stacktrace lines
StringWriter buf=new StringWriter();
PrintWriter stackout=new PrintWriter(buf);
t.printStackTrace(stackout);
//create the tokenizer to get each line of input
StringTokenizer st=new
StringTokenizer(buf.getBuffer().toString(),"\n");
while(st.hasMoreTokens())
{
m_out.println(st.nextToken());
m_out.println("<br>");
}
m_out.println("</p>");
This can be usefull... eg: I use it when I get something that I class
as a 500.
Nic
___________________________________________________________________________
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
--
This e-mail may contain confidential and/or privileged information. If you are not the
intended recipient (or have received this e-mail in error) please notify the sender
immediately and destroy this e-mail. Any unauthorised copying, disclosure or
distribution of the material in this e-mail is strictly forbidden.
___________________________________________________________________________
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