Andrea Caltroni <[EMAIL PROTECTED]> wrote:
>If a servlet generates an exception, where does the output go?
>Is it possible to see it?

Add a catch handler that intercepts all exceptions and logs them to a file.
For instance,

doGet()
{
  try {
   ...
  } catch (Exception e) {
    e.printStackTrace(System.err);
  }
}

System.err is either your jserv log file (set with the log.file property)
or your apache error log. You can also turn on stack tracing in jserv--in
the jserv properties file--so all exceptions go to the jserv log file.

If you don't want to waste time mucking around in your log files (I can't
stand doing this) you can change the code to return a messy full stack
trace to the browser by sending the stack trace to a string writer and then
writing the string to the servlet's output stream. Be sure to include some
nice message because your users won't know what to do with a stack trace.
Since you can't access environment variables (well, you can via  the system
properties but maybe you don't want to rely on this) then you can use a
couple of init args to set the site's main url and the webmaster's home
address.

For instance, this is what I'm using (for now):

Error

There was a problem accessing the site. You might try again later. You can
also go to this site's home page at http://www.ari.lan.

The remainder of this message contains ugly looking stuff that keeps
programmers busy. If you feel adventurous then you can try mailing this
whole message to the people who try to keep this site running at
mailto:[EMAIL PROTECTED].

... ugly stack trace ...

Here's the code I use to generate this ugly message (the
NestedExceptionInterface interface is my own thing used to wrap other
exceptions).

        /** If we got here it means we're really screwed, so just return
        /* a very basic error message. */
        public void handleBadException(HttpServletRequest httpRequest,
                HttpServletResponse httpResponse,
                int requestType,
                Exception e)
        {

                // log the exception so we can do a post mortem
                if (Log.ENABLED && Log.enabled(Log.ERROR_CHANNEL))
                        Log.println(Log.ERROR_CHANNEL, e);

                // get the actual error so we can try to explain what went
wrong
                Exception actualException = e;
                if (actualException instanceof NestedExceptionInterface) {
                        actualException = ((NestedExceptionInterface)
actualException).getNestedException();
                        if (actualException == null)
                                actualException = e;
                }

                // try to explain what went wrong
                String home = getInitParameter("home");
                String mail = getInitParameter("mail");
                StringBuffer message = new StringBuffer();
                message.append("<p>");
                if (actualException instanceof SQLException)
                        message.append("There was a problem accessing the
database.");
                else
                        message.append("There was a problem accessing the
site.");
                message.append(" You might try again later.");
                if (home != null)
                        message.append(" You can also go to this site's
home page at <a href=\"" + home + "\">" + home + "</a>.");
                message.append("</p>");
                message.append("<p>");
                message.append("The remainder of this message contains ugly
looking stuff that keeps programmers busy.");
                if (mail != null) {
                        message.append(" If you feel adventurous then you
can try mailing this whole message");
                        message.append(" to the people who try to keep this
site running at <a href=\"" + mail + "\">" + mail + "</a>");
                }
                message.append("</p>");

                // get the ugly stack trace so users can be scared
                StringWriter stackTrace = new StringWriter();
                actualException.printStackTrace(new PrintWriter(stackTrace));
                message.append("<pre>");
                message.append(stackTrace.toString());
                message.append("</pre>");

                // write a stupid generic error message
                try {
                        ServletUtil.writeMessage(httpResponse, "Error",
message.toString());
                } catch (IOException ex) {
                        // ignore this exception, which is most likely
caused by a broken
                        // connection
                }
        }
}

-- Ari Halberstadt mailto:[EMAIL PROTECTED] <http://www.magiccookie.com/>
PGP public key available at <http://www.magiccookie.com/pgpkey.txt>




----------------------------------------------------------------
To subscribe:        [EMAIL PROTECTED]
To unsubscribe:      [EMAIL PROTECTED]
READ THE FAQ!!!!     <http://java.apache.org/faq/>
Archives and Other:  <http://java.apache.org/main/mail.html/>
Problems?:           [EMAIL PROTECTED]

Reply via email to